@@ -63,6 +63,11 @@ func ValidateCmd(cmd *cobra.Command, args []string) error {
|
||||
// flags that do not necessarily change with context. These must be checked if
|
||||
// the caller explicitly changed the values.
|
||||
func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, error) {
|
||||
if flagSet.Changed(flags.FlagHome) {
|
||||
homeDir, _ := flagSet.GetString(flags.FlagHome)
|
||||
clientCtx = clientCtx.WithHomeDir(homeDir)
|
||||
}
|
||||
|
||||
if flagSet.Changed(flags.FlagChainID) {
|
||||
chainID, _ := flagSet.GetString(flags.FlagChainID)
|
||||
clientCtx = clientCtx.WithChainID(chainID)
|
||||
|
||||
@@ -1,191 +0,0 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
toml "github.com/pelletier/go-toml"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
)
|
||||
|
||||
const (
|
||||
flagGet = "get"
|
||||
)
|
||||
|
||||
var configDefaults = map[string]string{
|
||||
"chain-id": "",
|
||||
"keyring-backend": "os",
|
||||
"output": "text",
|
||||
"node": "tcp://localhost:26657",
|
||||
"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 {
|
||||
cmd := &cobra.Command{
|
||||
Use: "config <key> [value]",
|
||||
Short: "Get and set client options",
|
||||
Long: configCommandLongDescription(),
|
||||
RunE: runConfigCmd,
|
||||
Args: cobra.RangeArgs(0, 2),
|
||||
}
|
||||
|
||||
cmd.Flags().String(flags.FlagHome, defaultCLIHome,
|
||||
"set client's home directory for configuration")
|
||||
cmd.Flags().Bool(flagGet, false,
|
||||
"print configuration value or its default if unset")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runConfigCmd(cmd *cobra.Command, args []string) error {
|
||||
cfgFile, err := ensureConfFile(viper.GetString(flags.FlagHome))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
getAction := viper.GetBool(flagGet)
|
||||
if getAction && len(args) != 1 {
|
||||
return fmt.Errorf("wrong number of arguments")
|
||||
}
|
||||
|
||||
// load configuration
|
||||
tree, err := loadConfigFile(cmd, cfgFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// print the config and exit
|
||||
if len(args) == 0 {
|
||||
s, err := tree.ToTomlString()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd.Print(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
key := args[0]
|
||||
|
||||
// get config value for a given key
|
||||
if getAction {
|
||||
if defaultValue, ok := configBoolDefaults[key]; ok {
|
||||
cmd.Println(tree.GetDefault(key, defaultValue).(bool))
|
||||
return nil
|
||||
}
|
||||
|
||||
if defaultValue, ok := configDefaults[key]; ok {
|
||||
cmd.Println(tree.GetDefault(key, defaultValue).(string))
|
||||
return nil
|
||||
}
|
||||
|
||||
return errUnknownConfigKey(key)
|
||||
}
|
||||
|
||||
if len(args) != 2 {
|
||||
return fmt.Errorf("wrong number of arguments")
|
||||
}
|
||||
|
||||
value := args[1]
|
||||
|
||||
// set config value for a given key
|
||||
if _, ok := configBoolDefaults[key]; ok {
|
||||
boolVal, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tree.Set(key, boolVal)
|
||||
} else if _, ok := configDefaults[key]; ok {
|
||||
tree.Set(key, value)
|
||||
} else {
|
||||
return errUnknownConfigKey(key)
|
||||
}
|
||||
|
||||
// save configuration to disk
|
||||
if err := saveConfigFile(cfgFile, tree); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.PrintErrf("configuration saved to %s\n", cfgFile)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureConfFile(rootDir string) (string, error) {
|
||||
cfgPath := path.Join(rootDir, "config")
|
||||
if err := os.MkdirAll(cfgPath, os.ModePerm); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return path.Join(cfgPath, "config.toml"), nil
|
||||
}
|
||||
|
||||
func loadConfigFile(cmd *cobra.Command, cfgFile string) (*toml.Tree, error) {
|
||||
if _, err := os.Stat(cfgFile); os.IsNotExist(err) {
|
||||
cmd.PrintErrf("%s does not exist\n", cfgFile)
|
||||
return toml.Load(``)
|
||||
}
|
||||
|
||||
bz, err := ioutil.ReadFile(cfgFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tree, err := toml.LoadBytes(bz)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tree, nil
|
||||
}
|
||||
|
||||
func saveConfigFile(cfgFile string, tree io.WriterTo) error {
|
||||
fp, err := os.OpenFile(cfgFile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
_, err = tree.WriteTo(fp)
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"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
|
||||
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)
|
||||
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"}))
|
||||
}
|
||||
|
||||
func TestConfigCmd_UnknownOption(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)
|
||||
require.NotNil(t, cmd)
|
||||
require.Error(t, cmd.RunE(cmd, []string{"invalid", "true"}), "unknown configuration key: \"invalid\"")
|
||||
}
|
||||
|
||||
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)
|
||||
require.NotNil(t, cmd)
|
||||
|
||||
viper.Set(flagGet, true)
|
||||
require.NoError(t, cmd.RunE(cmd, []string{"offline"}))
|
||||
require.Contains(t, out.String(), "false")
|
||||
out.Reset()
|
||||
|
||||
viper.Set(flagGet, false)
|
||||
require.NoError(t, cmd.RunE(cmd, []string{"offline", "true"}))
|
||||
|
||||
viper.Set(flagGet, true)
|
||||
require.NoError(t, cmd.RunE(cmd, []string{"offline"}))
|
||||
require.Contains(t, out.String(), "true")
|
||||
}
|
||||
Reference in New Issue
Block a user