Skip to content

Commit

Permalink
client: show defaults when config help screen is displayed (#5896)
Browse files Browse the repository at this point in the history
Closes: #5895

Thanks: @njmurarka for pointing this out.
  • Loading branch information
Alessio Treglia authored Mar 31, 2020
1 parent fd2bb9a commit b239cc8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 23 additions & 1 deletion client/config.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -39,7 +41,8 @@ var configBoolDefaults = map[string]bool{
func ConfigCmd(defaultCLIHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "config <key> [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),
}
Expand Down Expand Up @@ -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)
}
}
43 changes: 23 additions & 20 deletions client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,19 +23,25 @@ func Test_runConfigCmdTwiceWithShorterNodeValue(t *testing.T) {

// Init command config
cmd := ConfigCmd(configHome)
assert.NotNil(t, cmd)

err := cmd.RunE(cmd, []string{"node", "tcp://localhost:26657"})
assert.Nil(t, err)
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", "--get"})
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", "tcp://local:26657"})
assert.Nil(t, err)
_ = os.RemoveAll(filepath.Join(configHome, "config"))
viper.Set(flags.FlagHome, configHome)

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) {
Expand All @@ -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")
}

0 comments on commit b239cc8

Please sign in to comment.