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

Closes: #5895

Thanks: @njmurarka for pointing this out.
This commit is contained in:
Alessio Treglia 2020-03-31 16:05:18 +02:00 committed by GitHub
parent fd2bb9aab6
commit b239cc8bb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 21 deletions

View File

@ -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

View File

@ -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 <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),
}
@ -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)
}
}

View File

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