diff --git a/CHANGELOG.md b/CHANGELOG.md index 33da257d94ac..14b54ec6d12c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -182,6 +182,7 @@ internet connection. Previously, `--generate-only` served this purpose in additi allows txs to be generated without being broadcasted and disallows Keybase use and `--offline` allows the use of Keybase but does not allow any functionality that requires an online connection. * (types/module) [\#5724](https://github.com/cosmos/cosmos-sdk/issues/5724) The `types/module` package does no longer depend on `x/simulation`. +* (client) [\#5856](https://github.com/cosmos/cosmos-sdk/pull/5856) Added the possibility to set `--offline` flag with config command. ## [v0.38.2] - 2020-03-25 diff --git a/client/config.go b/client/config.go index 226d056fea06..f4bb16b2a6b1 100644 --- a/client/config.go +++ b/client/config.go @@ -27,6 +27,13 @@ var configDefaults = map[string]string{ "broadcast-mode": "sync", } +var configBoolDefaults = map[string]bool{ + "trace": false, + "trust-node": false, + "indent": false, + "offline": false, +} + // ConfigCmd returns a CLI command to interactively create an application CLI // config file. func ConfigCmd(defaultCLIHome string) *cobra.Command { @@ -56,7 +63,7 @@ func runConfigCmd(cmd *cobra.Command, args []string) error { } // load configuration - tree, err := loadConfigFile(cfgFile) + tree, err := loadConfigFile(cmd, cfgFile) if err != nil { return err } @@ -67,7 +74,7 @@ func runConfigCmd(cmd *cobra.Command, args []string) error { if err != nil { return err } - fmt.Print(s) + cmd.Print(s) return nil } @@ -75,20 +82,17 @@ func runConfigCmd(cmd *cobra.Command, args []string) error { // get config value for a given key if getAction { - switch key { - case "trace", "trust-node", "indent": - fmt.Println(tree.GetDefault(key, false).(bool)) - - default: - if defaultValue, ok := configDefaults[key]; ok { - fmt.Println(tree.GetDefault(key, defaultValue).(string)) - return nil - } + if defaultValue, ok := configBoolDefaults[key]; ok { + cmd.Println(tree.GetDefault(key, defaultValue).(bool)) + return nil + } - return errUnknownConfigKey(key) + if defaultValue, ok := configDefaults[key]; ok { + cmd.Println(tree.GetDefault(key, defaultValue).(string)) + return nil } - return nil + return errUnknownConfigKey(key) } if len(args) != 2 { @@ -98,19 +102,16 @@ func runConfigCmd(cmd *cobra.Command, args []string) error { value := args[1] // set config value for a given key - switch key { - case "chain-id", "output", "node", "broadcast-mode", "keyring-backend": - tree.Set(key, value) - - case "trace", "trust-node", "indent": + if _, ok := configBoolDefaults[key]; ok { boolVal, err := strconv.ParseBool(value) if err != nil { return err } tree.Set(key, boolVal) - - default: + } else if _, ok := configDefaults[key]; ok { + tree.Set(key, value) + } else { return errUnknownConfigKey(key) } @@ -119,7 +120,8 @@ func runConfigCmd(cmd *cobra.Command, args []string) error { return err } - fmt.Fprintf(os.Stderr, "configuration saved to %s\n", cfgFile) + cmd.PrintErrf("configuration saved to %s\n", cfgFile) + return nil } @@ -132,9 +134,9 @@ func ensureConfFile(rootDir string) (string, error) { return path.Join(cfgPath, "config.toml"), nil } -func loadConfigFile(cfgFile string) (*toml.Tree, error) { +func loadConfigFile(cmd *cobra.Command, cfgFile string) (*toml.Tree, error) { if _, err := os.Stat(cfgFile); os.IsNotExist(err) { - fmt.Fprintf(os.Stderr, "%s does not exist\n", cfgFile) + cmd.PrintErrf("%s does not exist\n", cfgFile) return toml.Load(``) } diff --git a/client/config_test.go b/client/config_test.go index 242facaeea5f..178baa22f94b 100644 --- a/client/config_test.go +++ b/client/config_test.go @@ -1,24 +1,23 @@ package client import ( - "io/ioutil" "os" "path/filepath" "testing" "github.com/spf13/viper" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/tests" ) // For https://github.com/cosmos/cosmos-sdk/issues/3899 func Test_runConfigCmdTwiceWithShorterNodeValue(t *testing.T) { // Prepare environment - t.Parallel() - configHome, cleanup := tmpDir(t) - defer cleanup() + configHome, cleanup := tests.NewTestCaseDir(t) + t.Cleanup(cleanup) + _ = os.RemoveAll(filepath.Join(configHome, "config")) viper.Set(flags.FlagHome, configHome) @@ -39,8 +38,31 @@ func Test_runConfigCmdTwiceWithShorterNodeValue(t *testing.T) { assert.Nil(t, err) } -func tmpDir(t *testing.T) (string, func()) { - dir, err := ioutil.TempDir("", t.Name()+"_") - require.NoError(t, err) - return dir, func() { _ = os.RemoveAll(dir) } +func TestConfigCmd_OfflineFlag(t *testing.T) { + // Prepare environment + configHome, cleanup := tests.NewTestCaseDir(t) + t.Cleanup(cleanup) + + _ = os.RemoveAll(filepath.Join(configHome, "config")) + viper.Set(flags.FlagHome, configHome) + + // Init command config + cmd := ConfigCmd(configHome) + _, out, _ := tests.ApplyMockIO(cmd) + assert.NotNil(t, cmd) + + viper.Set(flagGet, true) + err := cmd.RunE(cmd, []string{"offline"}) + assert.Nil(t, err) + assert.Contains(t, out.String(), "false") + out.Reset() + + viper.Set(flagGet, false) + err = cmd.RunE(cmd, []string{"offline", "true"}) + assert.Nil(t, err) + + viper.Set(flagGet, true) + err = cmd.RunE(cmd, []string{"offline"}) + assert.Nil(t, err) + assert.Contains(t, out.String(), "true") }