2018-09-25 14:36:42 -07:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-11-07 16:42:34 -08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2018-12-04 05:17:45 -08:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/libs/cli"
|
2018-11-07 16:42:34 -08:00
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
|
|
|
|
|
2018-10-03 08:48:23 -07:00
|
|
|
"github.com/pelletier/go-toml"
|
|
|
|
"github.com/spf13/cobra"
|
2018-12-04 05:17:45 -08:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
flagGet = "get"
|
2018-09-25 14:36:42 -07:00
|
|
|
)
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
var configDefaults map[string]string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
configDefaults = map[string]string{
|
2018-12-18 10:40:01 -08:00
|
|
|
"chain-id": "",
|
2018-12-04 05:17:45 -08:00
|
|
|
"output": "text",
|
|
|
|
"node": "tcp://localhost:26657",
|
|
|
|
}
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigCmd returns a CLI command to interactively create a
|
|
|
|
// Gaia CLI config file.
|
|
|
|
func ConfigCmd() *cobra.Command {
|
2018-12-04 05:17:45 -08:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "config <key> [value]",
|
|
|
|
Short: "Create or query a Gaia CLI configuration file",
|
2018-09-25 14:36:42 -07:00
|
|
|
RunE: runConfigCmd,
|
2018-12-04 05:17:45 -08:00
|
|
|
Args: cobra.RangeArgs(0, 2),
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
cmd.Flags().String(cli.HomeFlag, app.DefaultCLIHome,
|
|
|
|
"set client's home directory for configuration")
|
|
|
|
cmd.Flags().Bool(flagGet, false,
|
|
|
|
"print configuration value or its default if unset")
|
|
|
|
return cmd
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
|
|
|
|
2018-10-03 08:48:23 -07:00
|
|
|
func runConfigCmd(cmd *cobra.Command, args []string) error {
|
2018-12-04 05:17:45 -08:00
|
|
|
cfgFile, err := ensureConfFile(viper.GetString(cli.HomeFlag))
|
2018-09-25 14:36:42 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
getAction := viper.GetBool(flagGet)
|
|
|
|
if getAction && len(args) != 1 {
|
|
|
|
return fmt.Errorf("wrong number of arguments")
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
2018-11-07 16:42:34 -08:00
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
// Load configuration
|
|
|
|
tree, err := loadConfigFile(cfgFile)
|
2018-09-25 14:36:42 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-07 16:42:34 -08:00
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
// Print the config and exit
|
|
|
|
if len(args) == 0 {
|
|
|
|
s, err := tree.ToTomlString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Print(s)
|
|
|
|
return nil
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
key := args[0]
|
|
|
|
// Get value action
|
|
|
|
if getAction {
|
|
|
|
switch key {
|
2019-01-22 02:07:18 -08:00
|
|
|
case "trace", "trust-node", "indent":
|
2018-12-04 05:17:45 -08:00
|
|
|
fmt.Println(tree.GetDefault(key, false).(bool))
|
|
|
|
default:
|
|
|
|
if defaultValue, ok := configDefaults[key]; ok {
|
|
|
|
fmt.Println(tree.GetDefault(key, defaultValue).(string))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errUnknownConfigKey(key)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-07 16:42:34 -08:00
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
// Set value action
|
2019-01-17 07:40:34 -08:00
|
|
|
if len(args) != 2 {
|
|
|
|
return fmt.Errorf("wrong number of arguments")
|
|
|
|
}
|
2018-12-04 05:17:45 -08:00
|
|
|
value := args[1]
|
|
|
|
switch key {
|
2018-12-18 10:40:01 -08:00
|
|
|
case "chain-id", "output", "node":
|
2018-12-04 05:17:45 -08:00
|
|
|
tree.Set(key, value)
|
2019-01-22 02:07:18 -08:00
|
|
|
case "trace", "trust-node", "indent":
|
2018-12-04 05:17:45 -08:00
|
|
|
boolVal, err := strconv.ParseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tree.Set(key, boolVal)
|
|
|
|
default:
|
|
|
|
return errUnknownConfigKey(key)
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
// Save configuration to disk
|
|
|
|
if err := saveConfigFile(cfgFile, tree); err != nil {
|
|
|
|
return err
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
2018-12-04 05:17:45 -08:00
|
|
|
fmt.Fprintf(os.Stderr, "configuration saved to %s\n", cfgFile)
|
2018-09-25 14:36:42 -07:00
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
return nil
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
func ensureConfFile(rootDir string) (string, error) {
|
|
|
|
cfgPath := path.Join(rootDir, "config")
|
|
|
|
if err := os.MkdirAll(cfgPath, os.ModePerm); err != nil {
|
2018-09-25 14:36:42 -07:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
return path.Join(cfgPath, "config.toml"), nil
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
func loadConfigFile(cfgFile string) (*toml.Tree, error) {
|
|
|
|
if _, err := os.Stat(cfgFile); os.IsNotExist(err) {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s does not exist\n", cfgFile)
|
|
|
|
return toml.Load(``)
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
bz, err := ioutil.ReadFile(cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
tree, err := toml.LoadBytes(bz)
|
2018-09-25 14:36:42 -07:00
|
|
|
if err != nil {
|
2018-12-04 05:17:45 -08:00
|
|
|
return nil, err
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveConfigFile(cfgFile string, tree *toml.Tree) error {
|
|
|
|
fp, err := os.OpenFile(cfgFile, os.O_WRONLY|os.O_CREATE, 0644)
|
2018-09-25 14:36:42 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-04 05:17:45 -08:00
|
|
|
defer fp.Close()
|
2018-09-25 14:36:42 -07:00
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
_, err = tree.WriteTo(fp)
|
|
|
|
return err
|
|
|
|
}
|
2018-09-25 14:36:42 -07:00
|
|
|
|
2018-12-04 05:17:45 -08:00
|
|
|
func errUnknownConfigKey(key string) error {
|
|
|
|
return fmt.Errorf("unknown configuration key: %q", key)
|
2018-09-25 14:36:42 -07:00
|
|
|
}
|