Skip to content

Commit

Permalink
client: support offline flag in config command (#5856)
Browse files Browse the repository at this point in the history
Update config hardcoded keys

Add test cases.

Co-authored-by: Alessio Treglia <[email protected]>
  • Loading branch information
jgimeno and Alessio Treglia authored Mar 30, 2020
1 parent 2a7a408 commit e5bde01
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 25 additions & 23 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -67,28 +74,25 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
fmt.Print(s)
cmd.Print(s)
return nil
}

key := args[0]

// 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
}
if defaultValue, ok := configBoolDefaults[key]; ok {
cmd.Println(tree.GetDefault(key, defaultValue).(bool))
return nil
}

return errUnknownConfigKey(key)
if defaultValue, ok := configDefaults[key]; ok {
cmd.Println(tree.GetDefault(key, defaultValue).(string))
return nil
}

return nil
return errUnknownConfigKey(key)
}

if len(args) != 2 {
Expand All @@ -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)
}

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

Expand All @@ -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(``)
}

Expand Down
40 changes: 31 additions & 9 deletions client/config_test.go
Original file line number Diff line number Diff line change
@@ -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)

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

0 comments on commit e5bde01

Please sign in to comment.