github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/wsctl/cmd/config/get.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/pkg/errors" 8 "github.com/spf13/cobra" 9 10 "github.com/machinefi/w3bstream/pkg/wsctl/client" 11 "github.com/machinefi/w3bstream/pkg/wsctl/config" 12 ) 13 14 var _validGetArgs = []string{"endpoint", "language", "all"} 15 16 var ( 17 _configGetUse = map[config.Language]string{ 18 config.English: "get VARIABLE", 19 config.Chinese: "get 变量", 20 } 21 _configGetUseCmdShorts = map[config.Language]string{ 22 config.English: "Get config fields from wsctl", 23 config.Chinese: "从 wsctl 获取配置字段", 24 } 25 _configGetUseCmdLong = map[config.Language]string{ 26 config.English: "Get config fields from wsctl\nValid Variables: [" + strings.Join(_validGetArgs, ", ") + "]", 27 config.Chinese: "从 wsctl 获取配置字段\n有效变量: [" + strings.Join(_validGetArgs, ", ") + "]", 28 } 29 ) 30 31 // newConfigGetCmd is a command to get config fields from wstcl. 32 func newConfigGetCmd(client client.Client) *cobra.Command { 33 return &cobra.Command{ 34 Use: client.SelectTranslation(_configGetUse), 35 Short: client.SelectTranslation(_configGetUseCmdShorts), 36 Long: client.SelectTranslation(_configGetUseCmdLong), 37 ValidArgs: _validGetArgs, 38 Args: cobra.ExactValidArgs(1), 39 RunE: func(cmd *cobra.Command, args []string) error { 40 cmd.SilenceUsage = true 41 info := info{config: client.Config(), configFile: client.ConfigFilePath()} 42 result, err := info.get(args[0]) 43 if err != nil { 44 return errors.Wrap(err, fmt.Sprintf("issue fetching config value %s", args[0])) 45 } 46 cmd.Println(result) 47 return nil 48 }, 49 } 50 } 51 52 // get retrieves a config item from its key. 53 func (c *info) get(arg string) (string, error) { 54 switch arg { 55 case "endpoint": 56 if c.config.Endpoint == "" { 57 return "", ErrEmptyEndpoint 58 } 59 return fmt.Sprintf("%s", c.config.Endpoint), nil 60 case "language": 61 return string(c.config.Language), nil 62 case "all": 63 return jsonString(c.config) 64 default: 65 return "", ErrConfigNotMatch 66 } 67 }