From 996573e36b15f35bf450966536abdff18dfdad2e Mon Sep 17 00:00:00 2001 From: Nick Pocock Date: Thu, 9 Jun 2022 15:18:34 +0200 Subject: [PATCH 01/10] Task: Return error from keep alive methods within httputil pkg --- pkg/util/httputil/httputil.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/util/httputil/httputil.go b/pkg/util/httputil/httputil.go index 323426655d..d5dbea833b 100644 --- a/pkg/util/httputil/httputil.go +++ b/pkg/util/httputil/httputil.go @@ -52,7 +52,14 @@ func (ln tcpKeepAliveListener) Accept() (net.Conn, error) { if err != nil { return nil, err } - tc.SetKeepAlive(true) - tc.SetKeepAlivePeriod(3 * time.Minute) + + if err = tc.SetKeepAlive(true); err != nil { + return nil, err + } + + if err = tc.SetKeepAlivePeriod(3 * time.Minute); err != nil { + return nil, err + } + return tc, nil } From 6c0e227ccee9eb15951cc163857a4c8b8a9617c8 Mon Sep 17 00:00:00 2001 From: Nick Pocock Date: Tue, 12 Jul 2022 15:12:16 +0200 Subject: [PATCH 02/10] Initial commit for get config task --- ioctl/config/config.go | 2 + ioctl/newcmd/config/config.go | 47 +++++++++++++ ioctl/newcmd/config/config_get.go | 51 ++++++++++++++ ioctl/newcmd/config/config_get_test.go | 1 + ioctl/newcmd/config/config_test.go | 96 ++++++++++++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 ioctl/newcmd/config/config_get_test.go diff --git a/ioctl/config/config.go b/ioctl/config/config.go index 2bd049ead2..2a9cc6fd61 100644 --- a/ioctl/config/config.go +++ b/ioctl/config/config.go @@ -32,6 +32,8 @@ var ( ErrConfigNotMatch = fmt.Errorf("no matching config") // ErrEmptyEndpoint indicates error for empty endpoint ErrEmptyEndpoint = fmt.Errorf("no endpoint has been set") + + ErrConfigDefaultAccount = fmt.Errorf("default account not set") ) // Language type used to enumerate supported language of ioctl diff --git a/ioctl/newcmd/config/config.go b/ioctl/newcmd/config/config.go index 6b06359f56..6078dd0184 100644 --- a/ioctl/newcmd/config/config.go +++ b/ioctl/newcmd/config/config.go @@ -8,6 +8,7 @@ package config import ( "fmt" + "github.com/iotexproject/iotex-core/ioctl/output" "os" "path" "path/filepath" @@ -127,6 +128,52 @@ func (c *info) reset() error { return nil } +// get retrieves a config item from its key. +func (c *info) get(arg string) (string, error) { + switch arg { + default: + return "", config.ErrConfigNotMatch + case "endpoint": + if c.readConfig.Endpoint == "" { + return "", config.ErrEmptyEndpoint + } + message := endpointMessage{Endpoint: c.readConfig.Endpoint, SecureConnect: c.readConfig.SecureConnect} + return message.String(), nil + case "wallet": + return c.readConfig.Wallet, nil + case "defaultacc": + if c.readConfig.DefaultAccount.AddressOrAlias == "" { + return "", config.ErrConfigDefaultAccount + } + return c.readConfig.DefaultAccount.String(), nil + case "explorer": + return c.readConfig.Explorer, nil + case "language": + return c.readConfig.Language, nil + case "nsv2height": + return strconv.FormatUint(c.readConfig.Nsv2height, 10), nil + case "analyserEndpoint": + return c.readConfig.AnalyserEndpoint, nil + case "all": + return c.readConfig.String(), nil + } + + return "", config.ErrConfigNotMatch +} + +type endpointMessage struct { + Endpoint string `json:"endpoint"` + SecureConnect bool `json:"secureConnect"` +} + +func (m *endpointMessage) String() string { + if output.Format == "" { + message := fmt.Sprint(m.Endpoint, " secure connect(TLS):", m.SecureConnect) + return message + } + return output.FormatString(output.Result, m) +} + // isSupportedLanguage checks if the language is a supported option and returns index when supported func (c *info) isSupportedLanguage(arg string) config.Language { if index, err := strconv.Atoi(arg); err == nil && index >= 0 && index < len(_supportedLanguage) { diff --git a/ioctl/newcmd/config/config_get.go b/ioctl/newcmd/config/config_get.go index 96afdd8d01..048af9c1a3 100644 --- a/ioctl/newcmd/config/config_get.go +++ b/ioctl/newcmd/config/config_get.go @@ -5,3 +5,54 @@ // License 2.0 that can be found in the LICENSE file. package config + +import ( + "fmt" + "github.com/pkg/errors" + "strings" + + "github.com/spf13/cobra" + + "github.com/iotexproject/iotex-core/ioctl" + "github.com/iotexproject/iotex-core/ioctl/config" +) + +var ( + _configUseCmdShorts = map[config.Language]string{ + config.English: "Get config fields from ioctl", + config.Chinese: "从 ioctl 获取配置字段", + } + _configUseCmdLong = map[config.Language]string{ + config.English: "Get config fields from ioctl\nValid Variables: [" + strings.Join(_validGetArgs, ", ") + "]", + config.Chinese: "从 ioctl 获取配置字段\n有效变量: [" + strings.Join(_validGetArgs, ", ") + "]", + } +) + +// NewConfigGetCmd is a command to get config fields from iotcl. +func NewConfigGetCmd(client ioctl.Client) *cobra.Command { + short, _ := client.SelectTranslation(_configUseCmdShorts) + long, _ := client.SelectTranslation(_configUseCmdLong) + + return &cobra.Command{ + Use: "get VARIABLE", + Short: short, + Long: long, + ValidArgs: _validGetArgs, + Args: func(cmd *cobra.Command, args []string) error { + if len(args) != 1 { + return fmt.Errorf("accepts 1 arg(s), received %d\n"+ + "Valid arg(s): %s", len(args), _validGetArgs) + } + return cobra.OnlyValidArgs(cmd, args) + }, + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + result, err := newInfo(client.Config(), "").get(args[0]) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("issue fetching config value %s", args[0])) + } + cmd.Println(result) + return nil + }, + } +} diff --git a/ioctl/newcmd/config/config_get_test.go b/ioctl/newcmd/config/config_get_test.go new file mode 100644 index 0000000000..d912156bec --- /dev/null +++ b/ioctl/newcmd/config/config_get_test.go @@ -0,0 +1 @@ +package config diff --git a/ioctl/newcmd/config/config_test.go b/ioctl/newcmd/config/config_test.go index a3bf3a2e22..f2f454b52a 100644 --- a/ioctl/newcmd/config/config_test.go +++ b/ioctl/newcmd/config/config_test.go @@ -7,12 +7,14 @@ package config import ( + "fmt" "os" "path/filepath" "testing" "github.com/stretchr/testify/require" + "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/testutil" ) @@ -31,3 +33,97 @@ func TestInitConfig(t *testing.T) { require.Equal(_supportedLanguage[0], cfg.Language) require.Equal(filepath.Join(testPath, _defaultConfigFileName), cfgFilePath) } + +func TestConfigGet(t *testing.T) { + require := require.New(t) + tempDir := os.TempDir() + testPath, err := os.MkdirTemp(tempDir, "testCfg") + require.NoError(err) + defer func() { + testutil.CleanupPath(testPath) + }() + _configDir = testPath + cfg, cfgFilePath, err := InitConfig() + // the endpoint & default account are blank strings within the initial config so I'm setting it here + cfg.Endpoint = "http://google.com" + cfg.DefaultAccount = config.Context{AddressOrAlias: "test"} + require.NoError(err) + + info := newInfo(cfg, cfgFilePath) + + tcs := []struct { + arg string + expected string + }{ + { + "endpoint", + "http://google.com secure connect(TLS):true", + }, + { + "wallet", + fmt.Sprintf("%s%s", tempDir, "testCfg"), + }, + { + "defaultacc", + "{\n \"addressOrAlias\": \"test\"\n}", + }, + { + "explorer", + "iotexscan", + }, + { + "language", + "English", + }, + { + "nsv2height", + "5165641", + }, + { + "analyserEndpoint", + "https://iotex-analyser-api-mainnet.chainanalytics.org", + }, + { + "all", + "\"endpoint\": \"http://google.com\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 5165641,\n \"analyserEndpoint\": \"https://iotex-analyser-api-mainnet.chainanalytics.org\"\n}", + }, + } + + for _, tc := range tcs { + cfgItem, err := info.get(tc.arg) + require.NoError(err) + require.Contains(cfgItem, tc.expected) + } +} + +func TestConfigGetError(t *testing.T) { + require := require.New(t) + tempDir := os.TempDir() + testPath, err := os.MkdirTemp(tempDir, "testCfg") + require.NoError(err) + defer func() { + testutil.CleanupPath(testPath) + }() + _configDir = testPath + cfg, cfgFilePath, err := InitConfig() + info := newInfo(cfg, cfgFilePath) + + tcs := []struct { + arg string + expected string + }{ + { + "endpoint", + "no endpoint has been set", + }, + { + "defaultacc", + "default account not set", + }, + } + for _, tc := range tcs { + _, err := info.get(tc.arg) + require.Error(err) + require.Contains(err.Error(), tc.expected) + } +} From c7a0f7cd2d1d67cafceac394f1032498661cfa94 Mon Sep 17 00:00:00 2001 From: Nick Pocock Date: Fri, 15 Jul 2022 10:45:05 +0200 Subject: [PATCH 03/10] Add unit tests --- ioctl/config/config.go | 4 ++-- ioctl/newcmd/config/config.go | 6 ++---- ioctl/newcmd/config/config_get.go | 17 +++++++++++------ ioctl/newcmd/config/config_get_test.go | 6 ++++++ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/ioctl/config/config.go b/ioctl/config/config.go index 2a9cc6fd61..8cbb97d03e 100644 --- a/ioctl/config/config.go +++ b/ioctl/config/config.go @@ -32,8 +32,8 @@ var ( ErrConfigNotMatch = fmt.Errorf("no matching config") // ErrEmptyEndpoint indicates error for empty endpoint ErrEmptyEndpoint = fmt.Errorf("no endpoint has been set") - - ErrConfigDefaultAccount = fmt.Errorf("default account not set") + // ErrConfigDefaultAccountNotSet indicates an error for the default account not being set + ErrConfigDefaultAccountNotSet = fmt.Errorf("default account not set") ) // Language type used to enumerate supported language of ioctl diff --git a/ioctl/newcmd/config/config.go b/ioctl/newcmd/config/config.go index 6078dd0184..28e73f563f 100644 --- a/ioctl/newcmd/config/config.go +++ b/ioctl/newcmd/config/config.go @@ -8,7 +8,6 @@ package config import ( "fmt" - "github.com/iotexproject/iotex-core/ioctl/output" "os" "path" "path/filepath" @@ -21,6 +20,7 @@ import ( serverCfg "github.com/iotexproject/iotex-core/config" "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/output" ) // Regexp patterns @@ -143,7 +143,7 @@ func (c *info) get(arg string) (string, error) { return c.readConfig.Wallet, nil case "defaultacc": if c.readConfig.DefaultAccount.AddressOrAlias == "" { - return "", config.ErrConfigDefaultAccount + return "", config.ErrConfigDefaultAccountNotSet } return c.readConfig.DefaultAccount.String(), nil case "explorer": @@ -157,8 +157,6 @@ func (c *info) get(arg string) (string, error) { case "all": return c.readConfig.String(), nil } - - return "", config.ErrConfigNotMatch } type endpointMessage struct { diff --git a/ioctl/newcmd/config/config_get.go b/ioctl/newcmd/config/config_get.go index 048af9c1a3..b0db3b3343 100644 --- a/ioctl/newcmd/config/config_get.go +++ b/ioctl/newcmd/config/config_get.go @@ -8,9 +8,9 @@ package config import ( "fmt" - "github.com/pkg/errors" "strings" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/iotexproject/iotex-core/ioctl" @@ -18,11 +18,15 @@ import ( ) var ( - _configUseCmdShorts = map[config.Language]string{ + _configGetUse = map[config.Language]string{ + config.English: "get VARIABLE", + config.Chinese: "获取变量", + } + _configGetUseCmdShorts = map[config.Language]string{ config.English: "Get config fields from ioctl", config.Chinese: "从 ioctl 获取配置字段", } - _configUseCmdLong = map[config.Language]string{ + _configGetUseCmdLong = map[config.Language]string{ config.English: "Get config fields from ioctl\nValid Variables: [" + strings.Join(_validGetArgs, ", ") + "]", config.Chinese: "从 ioctl 获取配置字段\n有效变量: [" + strings.Join(_validGetArgs, ", ") + "]", } @@ -30,11 +34,12 @@ var ( // NewConfigGetCmd is a command to get config fields from iotcl. func NewConfigGetCmd(client ioctl.Client) *cobra.Command { - short, _ := client.SelectTranslation(_configUseCmdShorts) - long, _ := client.SelectTranslation(_configUseCmdLong) + short, _ := client.SelectTranslation(_configGetUseCmdShorts) + long, _ := client.SelectTranslation(_configGetUseCmdLong) + use, _ := client.SelectTranslation(_configGetUse) return &cobra.Command{ - Use: "get VARIABLE", + Use: use, Short: short, Long: long, ValidArgs: _validGetArgs, diff --git a/ioctl/newcmd/config/config_get_test.go b/ioctl/newcmd/config/config_get_test.go index d912156bec..ce3a03d6e7 100644 --- a/ioctl/newcmd/config/config_get_test.go +++ b/ioctl/newcmd/config/config_get_test.go @@ -1 +1,7 @@ package config + +import "testing" + +func TestNewConfigGetCmd(t *testing.T) { + +} From e7b9f69ac41963486f75c27aa0d5df9c83208b3a Mon Sep 17 00:00:00 2001 From: Nick Pocock Date: Sat, 16 Jul 2022 07:46:35 +0200 Subject: [PATCH 04/10] Remove output package, change case default to last statement within switch, improve test setup --- ioctl/newcmd/config/config.go | 15 ++-------- ioctl/newcmd/config/config_get.go | 7 ++++- ioctl/newcmd/config/config_test.go | 48 ++++-------------------------- 3 files changed, 15 insertions(+), 55 deletions(-) diff --git a/ioctl/newcmd/config/config.go b/ioctl/newcmd/config/config.go index e2bb00a7f1..359b6ffab2 100644 --- a/ioctl/newcmd/config/config.go +++ b/ioctl/newcmd/config/config.go @@ -20,7 +20,6 @@ import ( serverCfg "github.com/iotexproject/iotex-core/config" "github.com/iotexproject/iotex-core/ioctl/config" - "github.com/iotexproject/iotex-core/ioctl/output" ) // Regexp patterns @@ -132,14 +131,12 @@ func (c *info) reset() error { // get retrieves a config item from its key. func (c *info) get(arg string) (string, error) { switch arg { - default: - return "", config.ErrConfigNotMatch case "endpoint": if c.readConfig.Endpoint == "" { return "", config.ErrEmptyEndpoint } message := endpointMessage{Endpoint: c.readConfig.Endpoint, SecureConnect: c.readConfig.SecureConnect} - return message.String(), nil + return fmt.Sprint(message.Endpoint, " secure connect(TLS):", message.SecureConnect), nil case "wallet": return c.readConfig.Wallet, nil case "defaultacc": @@ -157,6 +154,8 @@ func (c *info) get(arg string) (string, error) { return c.readConfig.AnalyserEndpoint, nil case "all": return c.readConfig.String(), nil + default: + return "", config.ErrConfigNotMatch } } @@ -165,14 +164,6 @@ type endpointMessage struct { SecureConnect bool `json:"secureConnect"` } -func (m *endpointMessage) String() string { - if output.Format == "" { - message := fmt.Sprint(m.Endpoint, " secure connect(TLS):", m.SecureConnect) - return message - } - return output.FormatString(output.Result, m) -} - // isSupportedLanguage checks if the language is a supported option and returns index when supported func (c *info) isSupportedLanguage(arg string) config.Language { if index, err := strconv.Atoi(arg); err == nil && index >= 0 && index < len(_supportedLanguage) { diff --git a/ioctl/newcmd/config/config_get.go b/ioctl/newcmd/config/config_get.go index d76840868b..38560c4a02 100644 --- a/ioctl/newcmd/config/config_get.go +++ b/ioctl/newcmd/config/config_get.go @@ -18,6 +18,10 @@ import ( ) var ( + _configGetUse = map[config.Language]string{ + config.English: "get VARIABLE", + config.Chinese: "get 变量", + } _configGetUseCmdShorts = map[config.Language]string{ config.English: "Get config fields from ioctl", config.Chinese: "从 ioctl 获取配置字段", @@ -30,11 +34,12 @@ var ( // NewConfigGetCmd is a command to get config fields from iotcl. func NewConfigGetCmd(client ioctl.Client) *cobra.Command { + use, _ := client.SelectTranslation(_configGetUse) short, _ := client.SelectTranslation(_configGetUseCmdShorts) long, _ := client.SelectTranslation(_configGetUseCmdLong) return &cobra.Command{ - Use: "VARIABLE", + Use: use, Short: short, Long: long, ValidArgs: _validGetArgs, diff --git a/ioctl/newcmd/config/config_test.go b/ioctl/newcmd/config/config_test.go index c453d64511..87ae60cf9e 100644 --- a/ioctl/newcmd/config/config_test.go +++ b/ioctl/newcmd/config/config_test.go @@ -8,14 +8,12 @@ package config import ( "fmt" - "os" "path/filepath" "testing" "github.com/stretchr/testify/require" "github.com/iotexproject/iotex-core/ioctl/config" - "github.com/iotexproject/iotex-core/testutil" ) func TestInitConfig(t *testing.T) { @@ -32,14 +30,10 @@ func TestInitConfig(t *testing.T) { func TestConfigGet(t *testing.T) { require := require.New(t) - tempDir := os.TempDir() - testPath, err := os.MkdirTemp(tempDir, "testCfg") - require.NoError(err) - defer func() { - testutil.CleanupPath(testPath) - }() + testPath := t.TempDir() _configDir = testPath cfg, cfgFilePath, err := InitConfig() + require.NoError(err) // the endpoint & default account are blank strings within the initial config, so I'm setting it here cfg.Endpoint = "http://google.com" cfg.DefaultAccount = config.Context{AddressOrAlias: "test"} @@ -57,7 +51,7 @@ func TestConfigGet(t *testing.T) { }, { "wallet", - fmt.Sprintf("%s%s", tempDir, "testCfg"), + testPath, }, { "defaultacc", @@ -87,43 +81,13 @@ func TestConfigGet(t *testing.T) { for _, tc := range tcs { cfgItem, err := info.get(tc.arg) - require.NoError(err) + if err != nil { + require.Contains(err.Error(), tc.expected) + } require.Contains(cfgItem, tc.expected) } } -func TestConfigGetError(t *testing.T) { - require := require.New(t) - tempDir := os.TempDir() - testPath, err := os.MkdirTemp(tempDir, "testCfg") - require.NoError(err) - defer func() { - testutil.CleanupPath(testPath) - }() - _configDir = testPath - cfg, cfgFilePath, err := InitConfig() - info := newInfo(cfg, cfgFilePath) - - tcs := []struct { - arg string - expected string - }{ - { - "endpoint", - "no endpoint has been set", - }, - { - "defaultacc", - "default account not set", - }, - } - for _, tc := range tcs { - _, err := info.get(tc.arg) - require.Error(err) - require.Contains(err.Error(), tc.expected) - } -} - func TestConfigReset(t *testing.T) { require := require.New(t) cfgDir := t.TempDir() From 05c7def60cfc4fc22e712afc4268b85ecab8b80b Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Tue, 19 Jul 2022 16:47:10 +0800 Subject: [PATCH 05/10] modify test --- ioctl/newcmd/config/config_test.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/ioctl/newcmd/config/config_test.go b/ioctl/newcmd/config/config_test.go index 87ae60cf9e..4a1445e74a 100644 --- a/ioctl/newcmd/config/config_test.go +++ b/ioctl/newcmd/config/config_test.go @@ -31,15 +31,15 @@ func TestInitConfig(t *testing.T) { func TestConfigGet(t *testing.T) { require := require.New(t) testPath := t.TempDir() - _configDir = testPath - cfg, cfgFilePath, err := InitConfig() - require.NoError(err) - // the endpoint & default account are blank strings within the initial config, so I'm setting it here - cfg.Endpoint = "http://google.com" - cfg.DefaultAccount = config.Context{AddressOrAlias: "test"} - require.NoError(err) - - info := newInfo(cfg, cfgFilePath) + info := newInfo(config.Config{ + Wallet: testPath, + SecureConnect: true, + Aliases: make(map[string]string), + DefaultAccount: config.Context{AddressOrAlias: "test"}, + Explorer: "iotexscan", + Language: "English", + AnalyserEndpoint: "testAnalyser", + }, testPath) tcs := []struct { arg string @@ -47,7 +47,7 @@ func TestConfigGet(t *testing.T) { }{ { "endpoint", - "http://google.com secure connect(TLS):true", + "no endpoint has been set", }, { "wallet", @@ -67,15 +67,15 @@ func TestConfigGet(t *testing.T) { }, { "nsv2height", - "5165641", + "0", }, { "analyserEndpoint", - "https://iotex-analyser-api-mainnet.chainanalytics.org", + "testAnalyser", }, { "all", - "\"endpoint\": \"http://google.com\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 5165641,\n \"analyserEndpoint\": \"https://iotex-analyser-api-mainnet.chainanalytics.org\"\n}", + "\"endpoint\": \"\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 0,\n \"analyserEndpoint\": \"testAnalyser\"\n}", }, } @@ -83,8 +83,9 @@ func TestConfigGet(t *testing.T) { cfgItem, err := info.get(tc.arg) if err != nil { require.Contains(err.Error(), tc.expected) + } else { + require.Contains(cfgItem, tc.expected) } - require.Contains(cfgItem, tc.expected) } } From 1baceb33e3d2b4df5905db106b3eda1012051986 Mon Sep 17 00:00:00 2001 From: Nick Pocock Date: Tue, 19 Jul 2022 15:51:24 +0200 Subject: [PATCH 06/10] Add jsonString method --- ioctl/newcmd/config/config.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ioctl/newcmd/config/config.go b/ioctl/newcmd/config/config.go index 359b6ffab2..a529ced168 100644 --- a/ioctl/newcmd/config/config.go +++ b/ioctl/newcmd/config/config.go @@ -7,7 +7,9 @@ package config import ( + "encoding/json" "fmt" + "log" "os" "path" "path/filepath" @@ -143,7 +145,7 @@ func (c *info) get(arg string) (string, error) { if c.readConfig.DefaultAccount.AddressOrAlias == "" { return "", config.ErrConfigDefaultAccountNotSet } - return c.readConfig.DefaultAccount.String(), nil + return JSONString(c.readConfig.DefaultAccount), nil case "explorer": return c.readConfig.Explorer, nil case "language": @@ -153,7 +155,7 @@ func (c *info) get(arg string) (string, error) { case "analyserEndpoint": return c.readConfig.AnalyserEndpoint, nil case "all": - return c.readConfig.String(), nil + return JSONString(c.readConfig), nil default: return "", config.ErrConfigNotMatch } @@ -200,3 +202,12 @@ func (c *info) loadConfig() error { } return nil } + +// JSONString returns json string for message +func JSONString(out interface{}) string { + byteAsJSON, err := json.MarshalIndent(out, "", " ") + if err != nil { + log.Panic(err) + } + return fmt.Sprint(string(byteAsJSON)) +} From d678d3d2bde75974b37d20b1232cb9f8073dd850 Mon Sep 17 00:00:00 2001 From: Nick Pocock Date: Tue, 19 Jul 2022 15:53:11 +0200 Subject: [PATCH 07/10] make json string unexported --- ioctl/newcmd/config/config.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ioctl/newcmd/config/config.go b/ioctl/newcmd/config/config.go index a529ced168..c13d40f024 100644 --- a/ioctl/newcmd/config/config.go +++ b/ioctl/newcmd/config/config.go @@ -145,7 +145,7 @@ func (c *info) get(arg string) (string, error) { if c.readConfig.DefaultAccount.AddressOrAlias == "" { return "", config.ErrConfigDefaultAccountNotSet } - return JSONString(c.readConfig.DefaultAccount), nil + return jsonString(c.readConfig.DefaultAccount), nil case "explorer": return c.readConfig.Explorer, nil case "language": @@ -155,7 +155,7 @@ func (c *info) get(arg string) (string, error) { case "analyserEndpoint": return c.readConfig.AnalyserEndpoint, nil case "all": - return JSONString(c.readConfig), nil + return jsonString(c.readConfig), nil default: return "", config.ErrConfigNotMatch } @@ -203,8 +203,8 @@ func (c *info) loadConfig() error { return nil } -// JSONString returns json string for message -func JSONString(out interface{}) string { +// jsonString returns json string for message +func jsonString(out interface{}) string { byteAsJSON, err := json.MarshalIndent(out, "", " ") if err != nil { log.Panic(err) From 4e8e15f5c1aaaca51d43b3c3f43bed5ec219ca2f Mon Sep 17 00:00:00 2001 From: Nick Pocock Date: Wed, 20 Jul 2022 08:14:01 +0200 Subject: [PATCH 08/10] Remove message struct and return endpoint data directly --- ioctl/newcmd/config/config.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/ioctl/newcmd/config/config.go b/ioctl/newcmd/config/config.go index c13d40f024..f90c8f32e7 100644 --- a/ioctl/newcmd/config/config.go +++ b/ioctl/newcmd/config/config.go @@ -137,8 +137,7 @@ func (c *info) get(arg string) (string, error) { if c.readConfig.Endpoint == "" { return "", config.ErrEmptyEndpoint } - message := endpointMessage{Endpoint: c.readConfig.Endpoint, SecureConnect: c.readConfig.SecureConnect} - return fmt.Sprint(message.Endpoint, " secure connect(TLS):", message.SecureConnect), nil + return fmt.Sprintf("%s secure connect(TLS): %t", c.readConfig.Endpoint, c.readConfig.SecureConnect), nil case "wallet": return c.readConfig.Wallet, nil case "defaultacc": @@ -161,11 +160,6 @@ func (c *info) get(arg string) (string, error) { } } -type endpointMessage struct { - Endpoint string `json:"endpoint"` - SecureConnect bool `json:"secureConnect"` -} - // isSupportedLanguage checks if the language is a supported option and returns index when supported func (c *info) isSupportedLanguage(arg string) config.Language { if index, err := strconv.Atoi(arg); err == nil && index >= 0 && index < len(_supportedLanguage) { From 9b6fbc125cf5b160574c200d1357058497834af8 Mon Sep 17 00:00:00 2001 From: Nick Pocock Date: Wed, 20 Jul 2022 08:17:14 +0200 Subject: [PATCH 09/10] Add wrapped erorr --- ioctl/newcmd/config/config.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ioctl/newcmd/config/config.go b/ioctl/newcmd/config/config.go index f90c8f32e7..5fb2520861 100644 --- a/ioctl/newcmd/config/config.go +++ b/ioctl/newcmd/config/config.go @@ -9,7 +9,6 @@ package config import ( "encoding/json" "fmt" - "log" "os" "path" "path/filepath" @@ -144,7 +143,7 @@ func (c *info) get(arg string) (string, error) { if c.readConfig.DefaultAccount.AddressOrAlias == "" { return "", config.ErrConfigDefaultAccountNotSet } - return jsonString(c.readConfig.DefaultAccount), nil + return jsonString(c.readConfig.DefaultAccount) case "explorer": return c.readConfig.Explorer, nil case "language": @@ -154,7 +153,7 @@ func (c *info) get(arg string) (string, error) { case "analyserEndpoint": return c.readConfig.AnalyserEndpoint, nil case "all": - return jsonString(c.readConfig), nil + return jsonString(c.readConfig) default: return "", config.ErrConfigNotMatch } @@ -198,10 +197,10 @@ func (c *info) loadConfig() error { } // jsonString returns json string for message -func jsonString(out interface{}) string { +func jsonString(out interface{}) (string, error) { byteAsJSON, err := json.MarshalIndent(out, "", " ") if err != nil { - log.Panic(err) + return "", errors.Wrap(err, "failed to JSON marshal config field") } - return fmt.Sprint(string(byteAsJSON)) + return fmt.Sprint(string(byteAsJSON)), nil } From 62cc50f51159e8c58f6743b775cfd84acf16483c Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Thu, 21 Jul 2022 13:50:53 +0800 Subject: [PATCH 10/10] use ExactValidArgs() --- ioctl/newcmd/config/config.go | 35 +++++++++++++------------- ioctl/newcmd/config/config_get.go | 8 +----- ioctl/newcmd/config/config_get_test.go | 2 +- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/ioctl/newcmd/config/config.go b/ioctl/newcmd/config/config.go index 5fb2520861..7d3e982dbe 100644 --- a/ioctl/newcmd/config/config.go +++ b/ioctl/newcmd/config/config.go @@ -10,7 +10,6 @@ import ( "encoding/json" "fmt" "os" - "path" "path/filepath" "regexp" "strconv" @@ -96,7 +95,7 @@ func InitConfig() (config.Config, string, error) { } } // Set language for ioctl - if info.isSupportedLanguage(info.readConfig.Language) == -1 { + if isSupportedLanguage(info.readConfig.Language) == -1 { fmt.Printf("Warn: Language %s is not supported, English instead.\n", info.readConfig.Language) } return info.readConfig, info.defaultConfigFile, nil @@ -112,7 +111,7 @@ func newInfo(readConfig config.Config, defaultConfigFile string) *info { // reset resets all values of config func (c *info) reset() error { - c.readConfig.Wallet = path.Dir(c.defaultConfigFile) + c.readConfig.Wallet = filepath.Dir(c.defaultConfigFile) c.readConfig.Endpoint = "" c.readConfig.SecureConnect = true c.readConfig.DefaultAccount = *new(config.Context) @@ -159,19 +158,6 @@ func (c *info) get(arg string) (string, error) { } } -// isSupportedLanguage checks if the language is a supported option and returns index when supported -func (c *info) isSupportedLanguage(arg string) config.Language { - if index, err := strconv.Atoi(arg); err == nil && index >= 0 && index < len(_supportedLanguage) { - return config.Language(index) - } - for i, lang := range _supportedLanguage { - if strings.EqualFold(arg, lang) { - return config.Language(i) - } - } - return config.Language(-1) -} - // writeConfig writes to config file func (c *info) writeConfig() error { out, err := yaml.Marshal(&c.readConfig) @@ -196,9 +182,22 @@ func (c *info) loadConfig() error { return nil } +// isSupportedLanguage checks if the language is a supported option and returns index when supported +func isSupportedLanguage(arg string) config.Language { + if index, err := strconv.Atoi(arg); err == nil && index >= 0 && index < len(_supportedLanguage) { + return config.Language(index) + } + for i, lang := range _supportedLanguage { + if strings.EqualFold(arg, lang) { + return config.Language(i) + } + } + return config.Language(-1) +} + // jsonString returns json string for message -func jsonString(out interface{}) (string, error) { - byteAsJSON, err := json.MarshalIndent(out, "", " ") +func jsonString(input interface{}) (string, error) { + byteAsJSON, err := json.MarshalIndent(input, "", " ") if err != nil { return "", errors.Wrap(err, "failed to JSON marshal config field") } diff --git a/ioctl/newcmd/config/config_get.go b/ioctl/newcmd/config/config_get.go index 38560c4a02..9182947200 100644 --- a/ioctl/newcmd/config/config_get.go +++ b/ioctl/newcmd/config/config_get.go @@ -43,13 +43,7 @@ func NewConfigGetCmd(client ioctl.Client) *cobra.Command { Short: short, Long: long, ValidArgs: _validGetArgs, - Args: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 { - return fmt.Errorf("accepts 1 arg(s), received %d\n"+ - "Valid arg(s): %s", len(args), _validGetArgs) - } - return cobra.OnlyValidArgs(cmd, args) - }, + Args: cobra.ExactValidArgs(1), RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true result, err := newInfo(client.Config(), client.ConfigFilePath()).get(args[0]) diff --git a/ioctl/newcmd/config/config_get_test.go b/ioctl/newcmd/config/config_get_test.go index 3aa7ebafaa..7ffa693f5a 100644 --- a/ioctl/newcmd/config/config_get_test.go +++ b/ioctl/newcmd/config/config_get_test.go @@ -6,7 +6,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" - + "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"