mirror of
https://github.com/tendermint/tendermint.git
synced 2026-07-30 12:02:53 +00:00
Merge branch 'develop' into stderr
This commit is contained in:
+14
-3
@@ -38,7 +38,7 @@ func PrepareBaseCmd(cmd *cobra.Command, envPrefix, defautRoot string) Executor {
|
||||
cmd.PersistentFlags().String(HomeFlag, "", "root 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}
|
||||
return Executor{cmd, os.Exit}
|
||||
}
|
||||
|
||||
// PrepareMainCmd is meant for client side libs that want some more flags
|
||||
@@ -58,7 +58,7 @@ func initEnv(prefix string) {
|
||||
|
||||
// env variables with TM prefix (eg. TM_ROOT)
|
||||
viper.SetEnvPrefix(prefix)
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
|
||||
viper.AutomaticEnv()
|
||||
}
|
||||
|
||||
@@ -82,6 +82,11 @@ func copyEnvVars(prefix string) {
|
||||
// Executor wraps the cobra Command with a nicer Execute method
|
||||
type Executor struct {
|
||||
*cobra.Command
|
||||
Exit func(int) // this is os.Exit by default, override in tests
|
||||
}
|
||||
|
||||
type ExitCoder interface {
|
||||
ExitCode() int
|
||||
}
|
||||
|
||||
// execute adds all child commands to the root command sets flags appropriately.
|
||||
@@ -91,12 +96,18 @@ func (e Executor) Execute() error {
|
||||
e.SilenceErrors = true
|
||||
err := e.Command.Execute()
|
||||
if err != nil {
|
||||
// TODO: something cooler with log-levels
|
||||
if viper.GetBool(TraceFlag) {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %+v\n", err)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||
}
|
||||
|
||||
// return error code 1 by default, can override it with a special error type
|
||||
exitCode := 1
|
||||
if ec, ok := err.(ExitCoder); ok {
|
||||
exitCode = ec.ExitCode()
|
||||
}
|
||||
e.Exit(exitCode)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
+25
-14
@@ -46,6 +46,7 @@ func TestSetupEnv(t *testing.T) {
|
||||
}
|
||||
demo.Flags().String("foobar", "", "Some test value from config")
|
||||
cmd := PrepareBaseCmd(demo, "DEMO", "/qwerty/asdfgh") // some missing dir..
|
||||
cmd.Exit = func(int) {}
|
||||
|
||||
viper.Reset()
|
||||
args := append([]string{cmd.Use}, tc.args...)
|
||||
@@ -63,47 +64,55 @@ func TestSetupConfig(t *testing.T) {
|
||||
cval1, cval2 := "fubble", "wubble"
|
||||
conf1, err := WriteDemoConfig(map[string]string{"boo": cval1})
|
||||
require.Nil(err)
|
||||
// even with some ignored fields, should be no problem
|
||||
conf2, err := WriteDemoConfig(map[string]string{"boo": cval2, "foo": "bar"})
|
||||
// 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(err)
|
||||
|
||||
cases := []struct {
|
||||
args []string
|
||||
env map[string]string
|
||||
expected string
|
||||
args []string
|
||||
env map[string]string
|
||||
expected string
|
||||
expectedTwo string
|
||||
}{
|
||||
{nil, nil, ""},
|
||||
{nil, nil, "", ""},
|
||||
// setting on the command line
|
||||
{[]string{"--boo", "haha"}, nil, "haha"},
|
||||
{[]string{"--root", conf1}, nil, cval1},
|
||||
{[]string{"--boo", "haha"}, nil, "haha", ""},
|
||||
{[]string{"--two-words", "rocks"}, nil, "", "rocks"},
|
||||
{[]string{"--root", conf1}, nil, cval1, ""},
|
||||
// test both variants of the prefix
|
||||
{nil, map[string]string{"RD_BOO": "bang"}, "bang"},
|
||||
{nil, map[string]string{"RD_ROOT": conf1}, cval1},
|
||||
{nil, map[string]string{"RDROOT": conf2}, cval2},
|
||||
{nil, map[string]string{"RDHOME": conf1}, cval1},
|
||||
{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{"RDHOME": conf1}, cval1, ""},
|
||||
// and when both are set??? HOME wins every time!
|
||||
{[]string{"--root", conf1}, map[string]string{"RDHOME": conf2}, cval2},
|
||||
{[]string{"--root", conf1}, map[string]string{"RDHOME": conf2}, cval2, "WORD"},
|
||||
}
|
||||
|
||||
for idx, tc := range cases {
|
||||
i := strconv.Itoa(idx)
|
||||
// test command that store value of foobar in local variable
|
||||
var foo string
|
||||
var foo, two string
|
||||
boo := &cobra.Command{
|
||||
Use: "reader",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
foo = viper.GetString("boo")
|
||||
two = viper.GetString("two-words")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
boo.Flags().String("boo", "", "Some test value from config")
|
||||
boo.Flags().String("two-words", "", "Check out env handling -")
|
||||
cmd := PrepareBaseCmd(boo, "RD", "/qwerty/asdfgh") // some missing dir...
|
||||
cmd.Exit = func(int) {}
|
||||
|
||||
viper.Reset()
|
||||
args := append([]string{cmd.Use}, tc.args...)
|
||||
err := RunWithArgs(cmd, args, tc.env)
|
||||
require.Nil(err, i)
|
||||
assert.Equal(tc.expected, foo, i)
|
||||
assert.Equal(tc.expectedTwo, two, i)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,6 +184,7 @@ func TestSetupUnmarshal(t *testing.T) {
|
||||
// from the default config here
|
||||
marsh.Flags().Int("age", base.Age, "Some test value from config")
|
||||
cmd := PrepareBaseCmd(marsh, "MR", "/qwerty/asdfgh") // some missing dir...
|
||||
cmd.Exit = func(int) {}
|
||||
|
||||
viper.Reset()
|
||||
args := append([]string{cmd.Use}, tc.args...)
|
||||
@@ -209,6 +219,7 @@ func TestSetupTrace(t *testing.T) {
|
||||
},
|
||||
}
|
||||
cmd := PrepareBaseCmd(trace, "DBG", "/qwerty/asdfgh") // some missing dir..
|
||||
cmd.Exit = func(int) {}
|
||||
|
||||
viper.Reset()
|
||||
args := append([]string{cmd.Use}, tc.args...)
|
||||
|
||||
Reference in New Issue
Block a user