github.com/Finschia/finschia-sdk@v0.48.1/client/config/cmd.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "path/filepath" 7 8 "github.com/spf13/cobra" 9 10 ostcli "github.com/Finschia/ostracon/libs/cli" 11 12 "github.com/Finschia/finschia-sdk/client" 13 "github.com/Finschia/finschia-sdk/client/flags" 14 ) 15 16 // Cmd returns a CLI command to interactively create an application CLI 17 // config file. 18 func Cmd() *cobra.Command { 19 cmd := &cobra.Command{ 20 Use: "config <key> [value]", 21 Short: "Create or query an application CLI configuration file", 22 RunE: runConfigCmd, 23 Args: cobra.RangeArgs(0, 2), 24 } 25 return cmd 26 } 27 28 func runConfigCmd(cmd *cobra.Command, args []string) error { 29 clientCtx := client.GetClientContextFromCmd(cmd) 30 configPath := filepath.Join(clientCtx.HomeDir, "config") 31 32 conf, err := getClientConfig(configPath, clientCtx.Viper) 33 if err != nil { 34 return fmt.Errorf("couldn't get client config: %v", err) 35 } 36 37 switch len(args) { 38 case 0: 39 // print all client config fields to stdout 40 s, err := json.MarshalIndent(conf, "", "\t") 41 if err != nil { 42 return err 43 } 44 cmd.Println(string(s)) 45 46 case 1: 47 // it's a get 48 key := args[0] 49 50 switch key { 51 case flags.FlagChainID: 52 cmd.Println(conf.ChainID) 53 case flags.FlagKeyringBackend: 54 cmd.Println(conf.KeyringBackend) 55 case ostcli.OutputFlag: 56 cmd.Println(conf.Output) 57 case flags.FlagNode: 58 cmd.Println(conf.Node) 59 case flags.FlagBroadcastMode: 60 cmd.Println(conf.BroadcastMode) 61 default: 62 err := errUnknownConfigKey(key) 63 return fmt.Errorf("couldn't get the value for the key: %v, error: %v", key, err) 64 } 65 66 case 2: 67 // it's set 68 key, value := args[0], args[1] 69 70 switch key { 71 case flags.FlagChainID: 72 conf.SetChainID(value) 73 case flags.FlagKeyringBackend: 74 conf.SetKeyringBackend(value) 75 case ostcli.OutputFlag: 76 conf.SetOutput(value) 77 case flags.FlagNode: 78 conf.SetNode(value) 79 case flags.FlagBroadcastMode: 80 conf.SetBroadcastMode(value) 81 default: 82 return errUnknownConfigKey(key) 83 } 84 85 confFile := filepath.Join(configPath, "client.toml") 86 if err := writeConfigToFile(confFile, conf); err != nil { 87 return fmt.Errorf("could not write client config to the file: %v", err) 88 } 89 90 default: 91 panic("cound not execute config command") 92 } 93 94 return nil 95 } 96 97 func errUnknownConfigKey(key string) error { 98 return fmt.Errorf("unknown configuration key: %q", key) 99 }