From b239cc8bb4e4458961ee8cb4a7d722d373f32d6a Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 31 Mar 2020 16:05:18 +0200 Subject: [PATCH] client: show defaults when config help screen is displayed (#5896) Closes: #5895 Thanks: @njmurarka for pointing this out. --- CHANGELOG.md | 1 + client/config.go | 24 +++++++++++++++++++++++- client/config_test.go | 43 +++++++++++++++++++++++-------------------- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb6bfb4f6..939bea0de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -185,6 +185,7 @@ allows txs to be generated without being broadcasted and disallows Keybase use a 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. +* (client) [\#5895](https://github.com/cosmos/cosmos-sdk/issues/5895) show config options in the config command's help screen. ## [v0.38.2] - 2020-03-25 diff --git a/client/config.go b/client/config.go index f4bb16b2a..68ff9c653 100644 --- a/client/config.go +++ b/client/config.go @@ -1,12 +1,14 @@ package client import ( + "bytes" "fmt" "io" "io/ioutil" "os" "path" "strconv" + "text/template" toml "github.com/pelletier/go-toml" "github.com/spf13/cobra" @@ -39,7 +41,8 @@ var configBoolDefaults = map[string]bool{ func ConfigCmd(defaultCLIHome string) *cobra.Command { cmd := &cobra.Command{ Use: "config [value]", - Short: "Create or query an application CLI configuration file", + Short: "Get and set client options", + Long: configCommandLongDescription(), RunE: runConfigCmd, Args: cobra.RangeArgs(0, 2), } @@ -167,3 +170,22 @@ func saveConfigFile(cfgFile string, tree io.WriterTo) error { func errUnknownConfigKey(key string) error { return fmt.Errorf("unknown configuration key: %q", key) } + +func configCommandLongDescription() string { + longDescTemplate := template.Must(template.New("configCommandLongDescription"). + Parse(`{{ range $key, $value := . }} {{ $key }} = {{ $value }} +{{ end }}`)) + defaultsTextBuffer := bytes.NewBufferString("") + must(longDescTemplate.Execute(defaultsTextBuffer, configDefaults)) + must(longDescTemplate.Execute(defaultsTextBuffer, configBoolDefaults)) + return fmt.Sprintf(`Display or change client application configuration values. + +Defaults: +%s`, defaultsTextBuffer) +} + +func must(err error) { + if err != nil { + panic(err) + } +} diff --git a/client/config_test.go b/client/config_test.go index 178baa22f..5a114a3e6 100644 --- a/client/config_test.go +++ b/client/config_test.go @@ -6,7 +6,7 @@ import ( "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" @@ -23,19 +23,25 @@ func Test_runConfigCmdTwiceWithShorterNodeValue(t *testing.T) { // Init command config cmd := ConfigCmd(configHome) - assert.NotNil(t, cmd) + require.NotNil(t, cmd) + require.NoError(t, cmd.RunE(cmd, []string{"node", "tcp://localhost:26657"})) + require.NoError(t, cmd.RunE(cmd, []string{"node", "--get"})) + require.NoError(t, cmd.RunE(cmd, []string{"node", "tcp://local:26657"})) + require.NoError(t, cmd.RunE(cmd, []string{"node", "--get"})) +} - err := cmd.RunE(cmd, []string{"node", "tcp://localhost:26657"}) - assert.Nil(t, err) +func TestConfigCmd_UnknownOption(t *testing.T) { + // Prepare environment + configHome, cleanup := tests.NewTestCaseDir(t) + t.Cleanup(cleanup) - err = cmd.RunE(cmd, []string{"node", "--get"}) - assert.Nil(t, err) + _ = os.RemoveAll(filepath.Join(configHome, "config")) + viper.Set(flags.FlagHome, configHome) - err = cmd.RunE(cmd, []string{"node", "tcp://local:26657"}) - assert.Nil(t, err) - - err = cmd.RunE(cmd, []string{"node", "--get"}) - assert.Nil(t, err) + // Init command config + cmd := ConfigCmd(configHome) + require.NotNil(t, cmd) + require.Error(t, cmd.RunE(cmd, []string{"invalid", "true"}), "unknown configuration key: \"invalid\"") } func TestConfigCmd_OfflineFlag(t *testing.T) { @@ -49,20 +55,17 @@ func TestConfigCmd_OfflineFlag(t *testing.T) { // Init command config cmd := ConfigCmd(configHome) _, out, _ := tests.ApplyMockIO(cmd) - assert.NotNil(t, cmd) + require.NotNil(t, cmd) viper.Set(flagGet, true) - err := cmd.RunE(cmd, []string{"offline"}) - assert.Nil(t, err) - assert.Contains(t, out.String(), "false") + require.NoError(t, cmd.RunE(cmd, []string{"offline"})) + require.Contains(t, out.String(), "false") out.Reset() viper.Set(flagGet, false) - err = cmd.RunE(cmd, []string{"offline", "true"}) - assert.Nil(t, err) + require.NoError(t, cmd.RunE(cmd, []string{"offline", "true"})) viper.Set(flagGet, true) - err = cmd.RunE(cmd, []string{"offline"}) - assert.Nil(t, err) - assert.Contains(t, out.String(), "true") + require.NoError(t, cmd.RunE(cmd, []string{"offline"})) + require.Contains(t, out.String(), "true") }