github.com/Finschia/finschia-sdk@v0.49.1/client/config/cmd.go (about)

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