@ -1,2 +1,3 @@ | |||
*.swp | |||
*.swo | |||
vendor |
@ -0,0 +1,84 @@ | |||
package cli | |||
import ( | |||
"bytes" | |||
"fmt" | |||
"io" | |||
"io/ioutil" | |||
"os" | |||
"path/filepath" | |||
) | |||
// WriteDemoConfig writes a toml file with the given values. | |||
// It returns the RootDir the config.toml file is stored in, | |||
// or an error if writing was impossible | |||
func WriteDemoConfig(vals map[string]string) (string, error) { | |||
cdir, err := ioutil.TempDir("", "test-cli") | |||
if err != nil { | |||
return "", err | |||
} | |||
data := "" | |||
for k, v := range vals { | |||
data = data + fmt.Sprintf("%s = \"%s\"\n", k, v) | |||
} | |||
cfile := filepath.Join(cdir, "config.toml") | |||
err = ioutil.WriteFile(cfile, []byte(data), 0666) | |||
return cdir, err | |||
} | |||
// RunWithArgs executes the given command with the specified command line args | |||
// and environmental variables set. It returns any error returned from cmd.Execute() | |||
func RunWithArgs(cmd Executable, args []string, env map[string]string) error { | |||
oargs := os.Args | |||
oenv := map[string]string{} | |||
// defer returns the environment back to normal | |||
defer func() { | |||
os.Args = oargs | |||
for k, v := range oenv { | |||
os.Setenv(k, v) | |||
} | |||
}() | |||
// set the args and env how we want them | |||
os.Args = args | |||
for k, v := range env { | |||
// backup old value if there, to restore at end | |||
oenv[k] = os.Getenv(k) | |||
err := os.Setenv(k, v) | |||
if err != nil { | |||
return err | |||
} | |||
} | |||
// and finally run the command | |||
return cmd.Execute() | |||
} | |||
// RunCaptureWithArgs executes the given command with the specified command line args | |||
// and environmental variables set. It returns whatever was writen to | |||
// stdout along with any error returned from cmd.Execute() | |||
func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (output string, err error) { | |||
old := os.Stdout // keep backup of the real stdout | |||
r, w, _ := os.Pipe() | |||
os.Stdout = w | |||
defer func() { | |||
os.Stdout = old // restoring the real stdout | |||
}() | |||
outC := make(chan string) | |||
// copy the output in a separate goroutine so printing can't block indefinitely | |||
go func() { | |||
var buf bytes.Buffer | |||
// io.Copy will end when we call w.Close() below | |||
io.Copy(&buf, r) | |||
outC <- buf.String() | |||
}() | |||
// now run the command | |||
err = RunWithArgs(cmd, args, env) | |||
// and grab the stdout to return | |||
w.Close() | |||
output = <-outC | |||
return output, err | |||
} |
@ -0,0 +1,178 @@ | |||
package cli | |||
import ( | |||
"fmt" | |||
"os" | |||
"strings" | |||
"github.com/pkg/errors" | |||
"github.com/spf13/cobra" | |||
"github.com/spf13/viper" | |||
data "github.com/tendermint/go-wire/data" | |||
"github.com/tendermint/go-wire/data/base58" | |||
) | |||
const ( | |||
RootFlag = "root" | |||
HomeFlag = "home" | |||
DebugFlag = "debug" | |||
OutputFlag = "output" | |||
EncodingFlag = "encoding" | |||
) | |||
// Executable is the minimal interface to *corba.Command, so we can | |||
// wrap if desired before the test | |||
type Executable interface { | |||
Execute() error | |||
} | |||
// PrepareBaseCmd is meant for tendermint and other servers | |||
func PrepareBaseCmd(cmd *cobra.Command, envPrefix, defautRoot 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().Bool(DebugFlag, false, "print out full stack trace on errors") | |||
cmd.PersistentPreRunE = concatCobraCmdFuncs(bindFlagsLoadViper, cmd.PersistentPreRunE) | |||
return Executor{cmd} | |||
} | |||
// PrepareMainCmd is meant for client side libs that want some more flags | |||
// | |||
// 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 { | |||
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) | |||
} | |||
// initEnv sets to use ENV variables if set. | |||
func initEnv(prefix string) { | |||
copyEnvVars(prefix) | |||
// env variables with TM prefix (eg. TM_ROOT) | |||
viper.SetEnvPrefix(prefix) | |||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | |||
viper.AutomaticEnv() | |||
} | |||
// This copies all variables like TMROOT to TM_ROOT, | |||
// so we can support both formats for the user | |||
func copyEnvVars(prefix string) { | |||
prefix = strings.ToUpper(prefix) | |||
ps := prefix + "_" | |||
for _, e := range os.Environ() { | |||
kv := strings.SplitN(e, "=", 2) | |||
if len(kv) == 2 { | |||
k, v := kv[0], kv[1] | |||
if strings.HasPrefix(k, prefix) && !strings.HasPrefix(k, ps) { | |||
k2 := strings.Replace(k, prefix, ps, 1) | |||
os.Setenv(k2, v) | |||
} | |||
} | |||
} | |||
} | |||
// Executor wraps the cobra Command with a nicer Execute method | |||
type Executor struct { | |||
*cobra.Command | |||
} | |||
// execute adds all child commands to the root command sets flags appropriately. | |||
// This is called by main.main(). It only needs to happen once to the rootCmd. | |||
func (e Executor) Execute() error { | |||
e.SilenceUsage = true | |||
e.SilenceErrors = true | |||
err := e.Command.Execute() | |||
if err != nil { | |||
// TODO: something cooler with log-levels | |||
if viper.GetBool(DebugFlag) { | |||
fmt.Printf("ERROR: %+v\n", err) | |||
} else { | |||
fmt.Println("ERROR:", err.Error()) | |||
} | |||
} | |||
return err | |||
} | |||
type cobraCmdFunc func(cmd *cobra.Command, args []string) error | |||
// Returns a single function that calls each argument function in sequence | |||
// RunE, PreRunE, PersistentPreRunE, etc. all have this same signature | |||
func concatCobraCmdFuncs(fs ...cobraCmdFunc) cobraCmdFunc { | |||
return func(cmd *cobra.Command, args []string) error { | |||
for _, f := range fs { | |||
if f != nil { | |||
if err := f(cmd, args); err != nil { | |||
return err | |||
} | |||
} | |||
} | |||
return nil | |||
} | |||
} | |||
// Bind all flags and read the config into viper | |||
func bindFlagsLoadViper(cmd *cobra.Command, args []string) error { | |||
// cmd.Flags() includes flags from this command and all persistent flags from the parent | |||
if err := viper.BindPFlags(cmd.Flags()); err != nil { | |||
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) | |||
} | |||
viper.SetConfigName("config") // name of config file (without extension) | |||
viper.AddConfigPath(rootDir) // search root directory | |||
// If a config file is found, read it in. | |||
if err := viper.ReadInConfig(); err == nil { | |||
// stderr, so if we redirect output to json file, this doesn't appear | |||
// fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) | |||
} else if _, ok := err.(viper.ConfigFileNotFoundError); !ok { | |||
// we ignore not found error, only parse error | |||
// stderr, so if we redirect output to json file, this doesn't appear | |||
fmt.Fprintf(os.Stderr, "%#v", err) | |||
} | |||
return nil | |||
} | |||
// setEncoding reads the encoding flag | |||
func setEncoding(cmd *cobra.Command, args []string) error { | |||
// validate and set encoding | |||
enc := viper.GetString("encoding") | |||
switch enc { | |||
case "hex": | |||
data.Encoder = data.HexEncoder | |||
case "b64": | |||
data.Encoder = data.B64Encoder | |||
case "btc": | |||
data.Encoder = base58.BTCEncoder | |||
default: | |||
return errors.Errorf("Unsupported encoding: %s", enc) | |||
} | |||
return nil | |||
} | |||
func validateOutput(cmd *cobra.Command, args []string) error { | |||
// validate output format | |||
output := viper.GetString(OutputFlag) | |||
switch output { | |||
case "text", "json": | |||
default: | |||
return errors.Errorf("Unsupported output format: %s", output) | |||
} | |||
return nil | |||
} |
@ -0,0 +1,226 @@ | |||
package cli | |||
import ( | |||
"fmt" | |||
"strconv" | |||
"strings" | |||
"testing" | |||
"github.com/pkg/errors" | |||
"github.com/spf13/cobra" | |||
"github.com/spf13/viper" | |||
"github.com/stretchr/testify/assert" | |||
"github.com/stretchr/testify/require" | |||
) | |||
func TestSetupEnv(t *testing.T) { | |||
assert, require := assert.New(t), require.New(t) | |||
cases := []struct { | |||
args []string | |||
env map[string]string | |||
expected string | |||
}{ | |||
{nil, nil, ""}, | |||
{[]string{"--foobar", "bang!"}, nil, "bang!"}, | |||
// make sure reset is good | |||
{nil, nil, ""}, | |||
// test both variants of the prefix | |||
{nil, map[string]string{"DEMO_FOOBAR": "good"}, "good"}, | |||
{nil, map[string]string{"DEMOFOOBAR": "silly"}, "silly"}, | |||
// and that cli overrides env... | |||
{[]string{"--foobar", "important"}, | |||
map[string]string{"DEMO_FOOBAR": "ignored"}, "important"}, | |||
} | |||
for idx, tc := range cases { | |||
i := strconv.Itoa(idx) | |||
// test command that store value of foobar in local variable | |||
var foo string | |||
demo := &cobra.Command{ | |||
Use: "demo", | |||
RunE: func(cmd *cobra.Command, args []string) error { | |||
foo = viper.GetString("foobar") | |||
return nil | |||
}, | |||
} | |||
demo.Flags().String("foobar", "", "Some test value from config") | |||
cmd := PrepareBaseCmd(demo, "DEMO", "/qwerty/asdfgh") // some missing dir.. | |||
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) | |||
} | |||
} | |||
func TestSetupConfig(t *testing.T) { | |||
assert, require := assert.New(t), require.New(t) | |||
// we pre-create two config files we can refer to in the rest of | |||
// the test cases. | |||
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"}) | |||
require.Nil(err) | |||
cases := []struct { | |||
args []string | |||
env map[string]string | |||
expected string | |||
}{ | |||
{nil, nil, ""}, | |||
// setting on the command line | |||
{[]string{"--boo", "haha"}, nil, "haha"}, | |||
{[]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}, | |||
// and when both are set??? HOME wins every time! | |||
{[]string{"--root", conf1}, map[string]string{"RDHOME": conf2}, cval2}, | |||
} | |||
for idx, tc := range cases { | |||
i := strconv.Itoa(idx) | |||
// test command that store value of foobar in local variable | |||
var foo string | |||
boo := &cobra.Command{ | |||
Use: "reader", | |||
RunE: func(cmd *cobra.Command, args []string) error { | |||
foo = viper.GetString("boo") | |||
return nil | |||
}, | |||
} | |||
boo.Flags().String("boo", "", "Some test value from config") | |||
cmd := PrepareBaseCmd(boo, "RD", "/qwerty/asdfgh") // some missing dir... | |||
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) | |||
} | |||
} | |||
type DemoConfig struct { | |||
Name string `mapstructure:"name"` | |||
Age int `mapstructure:"age"` | |||
Unused int `mapstructure:"unused"` | |||
} | |||
func TestSetupUnmarshal(t *testing.T) { | |||
assert, require := assert.New(t), require.New(t) | |||
// we pre-create two config files we can refer to in the rest of | |||
// the test cases. | |||
cval1, cval2 := "someone", "else" | |||
conf1, err := WriteDemoConfig(map[string]string{"name": cval1}) | |||
require.Nil(err) | |||
// even with some ignored fields, should be no problem | |||
conf2, err := WriteDemoConfig(map[string]string{"name": cval2, "foo": "bar"}) | |||
require.Nil(err) | |||
// unused is not declared on a flag and remains from base | |||
base := DemoConfig{ | |||
Name: "default", | |||
Age: 42, | |||
Unused: -7, | |||
} | |||
c := func(name string, age int) DemoConfig { | |||
r := base | |||
// anything set on the flags as a default is used over | |||
// the default config object | |||
r.Name = "from-flag" | |||
if name != "" { | |||
r.Name = name | |||
} | |||
if age != 0 { | |||
r.Age = age | |||
} | |||
return r | |||
} | |||
cases := []struct { | |||
args []string | |||
env map[string]string | |||
expected DemoConfig | |||
}{ | |||
{nil, nil, c("", 0)}, | |||
// setting on the command line | |||
{[]string{"--name", "haha"}, nil, c("haha", 0)}, | |||
{[]string{"--root", 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)}, | |||
{[]string{"--age", "17"}, map[string]string{"MRHOME": conf2}, c(cval2, 17)}, | |||
} | |||
for idx, tc := range cases { | |||
i := strconv.Itoa(idx) | |||
// test command that store value of foobar in local variable | |||
cfg := base | |||
marsh := &cobra.Command{ | |||
Use: "marsh", | |||
RunE: func(cmd *cobra.Command, args []string) error { | |||
return viper.Unmarshal(&cfg) | |||
}, | |||
} | |||
marsh.Flags().String("name", "from-flag", "Some test value from config") | |||
// if we want a flag to use the proper default, then copy it | |||
// 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... | |||
viper.Reset() | |||
args := append([]string{cmd.Use}, tc.args...) | |||
err := RunWithArgs(cmd, args, tc.env) | |||
require.Nil(err, i) | |||
assert.Equal(tc.expected, cfg, i) | |||
} | |||
} | |||
func TestSetupDebug(t *testing.T) { | |||
assert, require := assert.New(t), require.New(t) | |||
cases := []struct { | |||
args []string | |||
env map[string]string | |||
long bool | |||
expected string | |||
}{ | |||
{nil, nil, false, "Debug flag = false"}, | |||
{[]string{"--debug"}, nil, true, "Debug flag = true"}, | |||
{[]string{"--no-such-flag"}, nil, false, "unknown flag: --no-such-flag"}, | |||
{nil, map[string]string{"DBG_DEBUG": "true"}, true, "Debug flag = true"}, | |||
} | |||
for idx, tc := range cases { | |||
i := strconv.Itoa(idx) | |||
// test command that store value of foobar in local variable | |||
debug := &cobra.Command{ | |||
Use: "debug", | |||
RunE: func(cmd *cobra.Command, args []string) error { | |||
return errors.Errorf("Debug flag = %t", viper.GetBool(DebugFlag)) | |||
}, | |||
} | |||
cmd := PrepareBaseCmd(debug, "DBG", "/qwerty/asdfgh") // some missing dir.. | |||
viper.Reset() | |||
args := append([]string{cmd.Use}, tc.args...) | |||
out, err := RunCaptureWithArgs(cmd, args, tc.env) | |||
require.NotNil(err, i) | |||
msg := strings.Split(out, "\n") | |||
desired := fmt.Sprintf("ERROR: %s", tc.expected) | |||
assert.Equal(desired, msg[0], i) | |||
if tc.long && assert.True(len(msg) > 2, i) { | |||
// the next line starts the stack trace... | |||
assert.Contains(msg[1], "TestSetupDebug", i) | |||
assert.Contains(msg[2], "setup_test.go", i) | |||
} | |||
} | |||
} |