Browse Source

Merge pull request #97 from tendermint/rm-root-flag

remove deprecated --root flag
pull/1842/head
Anton Kaliaev 7 years ago
committed by GitHub
parent
commit
a80b66268f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 31 deletions
  1. +7
    -20
      cli/setup.go
  2. +5
    -11
      cli/setup_test.go

+ 7
- 20
cli/setup.go View File

@ -14,7 +14,6 @@ import (
)
const (
RootFlag = "root"
HomeFlag = "home"
TraceFlag = "trace"
OutputFlag = "output"
@ -28,14 +27,9 @@ type Executable interface {
}
// PrepareBaseCmd is meant for tendermint and other servers
func PrepareBaseCmd(cmd *cobra.Command, envPrefix, defautRoot string) Executor {
func PrepareBaseCmd(cmd *cobra.Command, envPrefix, defaultHome string) Executor {
cobra.OnInitialize(func() { initEnv(envPrefix) })
cmd.PersistentFlags().StringP(RootFlag, "r", defautRoot, "DEPRECATED. Use --home")
// -h is already reserved for --help as part of the cobra framework
// do you want to try something else??
// also, default must be empty, so we can detect this unset and fall back
// to --root / TM_ROOT / TMROOT
cmd.PersistentFlags().String(HomeFlag, "", "root directory for config and data")
cmd.PersistentFlags().StringP(HomeFlag, "", defaultHome, "directory for config and data")
cmd.PersistentFlags().Bool(TraceFlag, false, "print out full stack trace on errors")
cmd.PersistentPreRunE = concatCobraCmdFuncs(bindFlagsLoadViper, cmd.PersistentPreRunE)
return Executor{cmd, os.Exit}
@ -45,11 +39,11 @@ func PrepareBaseCmd(cmd *cobra.Command, envPrefix, defautRoot string) Executor {
//
// This adds --encoding (hex, btc, base64) and --output (text, json) to
// the command. These only really make sense in interactive commands.
func PrepareMainCmd(cmd *cobra.Command, envPrefix, defautRoot string) Executor {
func PrepareMainCmd(cmd *cobra.Command, envPrefix, defaultHome string) Executor {
cmd.PersistentFlags().StringP(EncodingFlag, "e", "hex", "Binary encoding (hex|b64|btc)")
cmd.PersistentFlags().StringP(OutputFlag, "o", "text", "Output format (text|json)")
cmd.PersistentPreRunE = concatCobraCmdFuncs(setEncoding, validateOutput, cmd.PersistentPreRunE)
return PrepareBaseCmd(cmd, envPrefix, defautRoot)
return PrepareBaseCmd(cmd, envPrefix, defaultHome)
}
// initEnv sets to use ENV variables if set.
@ -136,17 +130,10 @@ func bindFlagsLoadViper(cmd *cobra.Command, args []string) error {
return err
}
// rootDir is command line flag, env variable, or default $HOME/.tlc
// NOTE: we support both --root and --home for now, but eventually only --home
// Also ensure we set the correct rootDir under HomeFlag so we dont need to
// repeat this logic elsewhere.
rootDir := viper.GetString(HomeFlag)
if rootDir == "" {
rootDir = viper.GetString(RootFlag)
viper.Set(HomeFlag, rootDir)
}
homeDir := viper.GetString(HomeFlag)
viper.Set(HomeFlag, homeDir)
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath(rootDir) // search root directory
viper.AddConfigPath(homeDir) // search root directory
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {


+ 5
- 11
cli/setup_test.go View File

@ -57,12 +57,9 @@ func TestSetupEnv(t *testing.T) {
func TestSetupConfig(t *testing.T) {
// we pre-create two config files we can refer to in the rest of
// the test cases.
cval1, cval2 := "fubble", "wubble"
cval1 := "fubble"
conf1, err := WriteDemoConfig(map[string]string{"boo": cval1})
require.Nil(t, err)
// make sure it handles dashed-words in the config, and ignores random info
conf2, err := WriteDemoConfig(map[string]string{"boo": cval2, "foo": "bar", "two-words": "WORD"})
require.Nil(t, err)
cases := []struct {
args []string
@ -74,16 +71,13 @@ func TestSetupConfig(t *testing.T) {
// setting on the command line
{[]string{"--boo", "haha"}, nil, "haha", ""},
{[]string{"--two-words", "rocks"}, nil, "", "rocks"},
{[]string{"--root", conf1}, nil, cval1, ""},
{[]string{"--home", conf1}, nil, cval1, ""},
// test both variants of the prefix
{nil, map[string]string{"RD_BOO": "bang"}, "bang", ""},
{nil, map[string]string{"RD_TWO_WORDS": "fly"}, "", "fly"},
{nil, map[string]string{"RDTWO_WORDS": "fly"}, "", "fly"},
{nil, map[string]string{"RD_ROOT": conf1}, cval1, ""},
{nil, map[string]string{"RDROOT": conf2}, cval2, "WORD"},
{nil, map[string]string{"RD_HOME": conf1}, cval1, ""},
{nil, map[string]string{"RDHOME": conf1}, cval1, ""},
// and when both are set??? HOME wins every time!
{[]string{"--root", conf1}, map[string]string{"RDHOME": conf2}, cval2, "WORD"},
}
for idx, tc := range cases {
@ -156,10 +150,10 @@ func TestSetupUnmarshal(t *testing.T) {
{nil, nil, c("", 0)},
// setting on the command line
{[]string{"--name", "haha"}, nil, c("haha", 0)},
{[]string{"--root", conf1}, nil, c(cval1, 0)},
{[]string{"--home", conf1}, nil, c(cval1, 0)},
// test both variants of the prefix
{nil, map[string]string{"MR_AGE": "56"}, c("", 56)},
{nil, map[string]string{"MR_ROOT": conf1}, c(cval1, 0)},
{nil, map[string]string{"MR_HOME": conf1}, c(cval1, 0)},
{[]string{"--age", "17"}, map[string]string{"MRHOME": conf2}, c(cval2, 17)},
}


Loading…
Cancel
Save