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: support offline flag in config command #5856

Merged
merged 6 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -179,6 +179,7 @@ be executed without an internet connection. Previously, `--generate-only` served
txs to be generated. Now, `--generate-only` solely 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.1] - 2020-02-11

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
1 change: 1 addition & 0 deletions client/config/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
offline = true
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
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")
}