2017-05-02 08:01:50 -07:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2017-05-02 11:57:32 -07:00
|
|
|
|
2017-05-02 08:01:50 -07:00
|
|
|
data "github.com/tendermint/go-wire/data"
|
|
|
|
"github.com/tendermint/go-wire/data/base58"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
RootFlag = "root"
|
2017-05-02 11:50:24 -07:00
|
|
|
HomeFlag = "home"
|
2017-05-03 02:25:07 -07:00
|
|
|
DebugFlag = "debug"
|
2017-05-02 08:01:50 -07:00
|
|
|
OutputFlag = "output"
|
|
|
|
EncodingFlag = "encoding"
|
|
|
|
)
|
|
|
|
|
2017-05-03 02:25:07 -07:00
|
|
|
// Executable is the minimal interface to *corba.Command, so we can
|
|
|
|
// wrap if desired before the test
|
|
|
|
type Executable interface {
|
|
|
|
Execute() error
|
|
|
|
}
|
|
|
|
|
2017-05-02 08:01:50 -07:00
|
|
|
// PrepareBaseCmd is meant for tendermint and other servers
|
2017-05-03 02:25:07 -07:00
|
|
|
func PrepareBaseCmd(cmd *cobra.Command, envPrefix, defautRoot string) Executor {
|
2017-05-02 08:01:50 -07:00
|
|
|
cobra.OnInitialize(func() { initEnv(envPrefix) })
|
2017-05-02 11:50:24 -07:00
|
|
|
cmd.PersistentFlags().StringP(RootFlag, "r", defautRoot, "DEPRECATED. Use --home")
|
2017-05-03 01:37:25 -07:00
|
|
|
// -h is already reserved for --help as part of the cobra framework
|
2017-05-03 02:25:07 -07:00
|
|
|
// 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
|
2017-05-03 01:37:25 -07:00
|
|
|
cmd.PersistentFlags().String(HomeFlag, "", "root directory for config and data")
|
2017-05-03 02:25:07 -07:00
|
|
|
cmd.PersistentFlags().Bool(DebugFlag, false, "print out full stack trace on errors")
|
2017-05-02 11:57:32 -07:00
|
|
|
cmd.PersistentPreRunE = concatCobraCmdFuncs(bindFlagsLoadViper, cmd.PersistentPreRunE)
|
2017-05-03 02:25:07 -07:00
|
|
|
return Executor{cmd}
|
2017-05-02 08:01:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareMainCmd is meant for client side libs that want some more flags
|
2017-05-03 00:25:04 -07:00
|
|
|
//
|
|
|
|
// This adds --encoding (hex, btc, base64) and --output (text, json) to
|
|
|
|
// the command. These only really make sense in interactive commands.
|
2017-05-03 02:25:07 -07:00
|
|
|
func PrepareMainCmd(cmd *cobra.Command, envPrefix, defautRoot string) Executor {
|
2017-05-02 08:01:50 -07:00
|
|
|
cmd.PersistentFlags().StringP(EncodingFlag, "e", "hex", "Binary encoding (hex|b64|btc)")
|
|
|
|
cmd.PersistentFlags().StringP(OutputFlag, "o", "text", "Output format (text|json)")
|
2017-05-02 11:57:32 -07:00
|
|
|
cmd.PersistentPreRunE = concatCobraCmdFuncs(setEncoding, validateOutput, cmd.PersistentPreRunE)
|
2017-05-02 08:01:50 -07:00
|
|
|
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() {
|
2017-05-02 08:16:22 -07:00
|
|
|
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)
|
|
|
|
}
|
2017-05-02 08:01:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 02:25:07 -07:00
|
|
|
// Executor wraps the cobra Command with a nicer Execute method
|
|
|
|
type Executor struct {
|
|
|
|
*cobra.Command
|
|
|
|
}
|
|
|
|
|
2017-05-02 08:01:50 -07:00
|
|
|
// 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.
|
2017-05-03 02:25:07 -07:00
|
|
|
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())
|
|
|
|
}
|
2017-05-02 08:01:50 -07:00
|
|
|
}
|
2017-05-03 02:25:07 -07:00
|
|
|
return err
|
2017-05-02 08:01:50 -07:00
|
|
|
}
|
|
|
|
|
2017-05-02 11:57:32 -07:00
|
|
|
type cobraCmdFunc func(cmd *cobra.Command, args []string) error
|
2017-05-02 08:01:50 -07:00
|
|
|
|
2017-05-02 11:57:32 -07:00
|
|
|
// Returns a single function that calls each argument function in sequence
|
2017-05-03 00:25:04 -07:00
|
|
|
// RunE, PreRunE, PersistentPreRunE, etc. all have this same signature
|
2017-05-02 11:57:32 -07:00
|
|
|
func concatCobraCmdFuncs(fs ...cobraCmdFunc) cobraCmdFunc {
|
2017-05-02 08:01:50 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:57:32 -07:00
|
|
|
// Bind all flags and read the config into viper
|
|
|
|
func bindFlagsLoadViper(cmd *cobra.Command, args []string) error {
|
2017-05-02 08:01:50 -07:00
|
|
|
// 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
|
2017-05-02 11:50:24 -07:00
|
|
|
// NOTE: we support both --root and --home for now, but eventually only --home
|
|
|
|
rootDir := viper.GetString(HomeFlag)
|
2017-05-03 01:37:25 -07:00
|
|
|
// @ebuchman: viper.IsSet doesn't do what you think...
|
|
|
|
// Even a default of "" on the pflag marks it as set,
|
|
|
|
// simply by fact of having a pflag.
|
|
|
|
if rootDir == "" {
|
2017-05-02 11:50:24 -07:00
|
|
|
rootDir = viper.GetString(RootFlag)
|
|
|
|
}
|
2017-05-02 08:01:50 -07:00
|
|
|
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
|
|
|
|
}
|