client: support offline flag in config command (#5856)

Update config hardcoded keys

Add test cases.

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
This commit is contained in:
Jonathan Gimeno 2020-03-30 23:55:28 +02:00 committed by GitHub
parent 2a7a408d35
commit e5bde0199e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 33 deletions

View File

@ -182,6 +182,7 @@ internet connection. Previously, `--generate-only` served this purpose in additi
allows txs to be generated without being broadcasted and disallows Keybase use and `--offline` allows the use of Keybase but does not allow any
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.
## [v0.38.2] - 2020-03-25

View File

@ -27,6 +27,13 @@ var configDefaults = map[string]string{
"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 {
@ -56,7 +63,7 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
}
// load configuration
tree, err := loadConfigFile(cfgFile)
tree, err := loadConfigFile(cmd, cfgFile)
if err != nil {
return err
}
@ -67,7 +74,7 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
fmt.Print(s)
cmd.Print(s)
return nil
}
@ -75,20 +82,17 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
// get config value for a given key
if getAction {
switch key {
case "trace", "trust-node", "indent":
fmt.Println(tree.GetDefault(key, false).(bool))
default:
if defaultValue, ok := configDefaults[key]; ok {
fmt.Println(tree.GetDefault(key, defaultValue).(string))
return nil
}
return errUnknownConfigKey(key)
if defaultValue, ok := configBoolDefaults[key]; ok {
cmd.Println(tree.GetDefault(key, defaultValue).(bool))
return nil
}
return nil
if defaultValue, ok := configDefaults[key]; ok {
cmd.Println(tree.GetDefault(key, defaultValue).(string))
return nil
}
return errUnknownConfigKey(key)
}
if len(args) != 2 {
@ -98,19 +102,16 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
value := args[1]
// set config value for a given key
switch key {
case "chain-id", "output", "node", "broadcast-mode", "keyring-backend":
tree.Set(key, value)
case "trace", "trust-node", "indent":
if _, ok := configBoolDefaults[key]; ok {
boolVal, err := strconv.ParseBool(value)
if err != nil {
return err
}
tree.Set(key, boolVal)
default:
} else if _, ok := configDefaults[key]; ok {
tree.Set(key, value)
} else {
return errUnknownConfigKey(key)
}
@ -119,7 +120,8 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
return err
}
fmt.Fprintf(os.Stderr, "configuration saved to %s\n", cfgFile)
cmd.PrintErrf("configuration saved to %s\n", cfgFile)
return nil
}
@ -132,9 +134,9 @@ func ensureConfFile(rootDir string) (string, error) {
return path.Join(cfgPath, "config.toml"), nil
}
func loadConfigFile(cfgFile string) (*toml.Tree, error) {
func loadConfigFile(cmd *cobra.Command, cfgFile string) (*toml.Tree, error) {
if _, err := os.Stat(cfgFile); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "%s does not exist\n", cfgFile)
cmd.PrintErrf("%s does not exist\n", cfgFile)
return toml.Load(``)
}

View File

@ -1,24 +1,23 @@
package client
import (
"io/ioutil"
"os"
"path/filepath"
"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"
)
// For https://github.com/cosmos/cosmos-sdk/issues/3899
func Test_runConfigCmdTwiceWithShorterNodeValue(t *testing.T) {
// Prepare environment
t.Parallel()
configHome, cleanup := tmpDir(t)
defer cleanup()
configHome, cleanup := tests.NewTestCaseDir(t)
t.Cleanup(cleanup)
_ = os.RemoveAll(filepath.Join(configHome, "config"))
viper.Set(flags.FlagHome, configHome)
@ -39,8 +38,31 @@ func Test_runConfigCmdTwiceWithShorterNodeValue(t *testing.T) {
assert.Nil(t, err)
}
func tmpDir(t *testing.T) (string, func()) {
dir, err := ioutil.TempDir("", t.Name()+"_")
require.NoError(t, err)
return dir, func() { _ = os.RemoveAll(dir) }
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)
assert.NotNil(t, cmd)
viper.Set(flagGet, true)
err := cmd.RunE(cmd, []string{"offline"})
assert.Nil(t, err)
assert.Contains(t, out.String(), "false")
out.Reset()
viper.Set(flagGet, false)
err = cmd.RunE(cmd, []string{"offline", "true"})
assert.Nil(t, err)
viper.Set(flagGet, true)
err = cmd.RunE(cmd, []string{"offline"})
assert.Nil(t, err)
assert.Contains(t, out.String(), "true")
}