Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: show defaults when config help screen is displayed #5896

Merged
merged 2 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}