github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/wsctl/cmd/config/set.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/spf13/cobra"
     9  	"golang.org/x/text/cases"
    10  	"golang.org/x/text/language"
    11  
    12  	"github.com/machinefi/w3bstream/pkg/wsctl/client"
    13  	"github.com/machinefi/w3bstream/pkg/wsctl/config"
    14  )
    15  
    16  var _validArgs = []string{"endpoint", "language"}
    17  
    18  var (
    19  	_configSetUse = map[config.Language]string{
    20  		config.English: "set VARIABLE VALUE",
    21  		config.Chinese: "set 变量 值",
    22  	}
    23  	_configSetUseCmdShorts = map[config.Language]string{
    24  		config.English: "Set config fields for wsctl",
    25  		config.Chinese: "为 wsctl 设置配置字段",
    26  	}
    27  	_configSetUseCmdLong = map[config.Language]string{
    28  		config.English: "Set config fields for wsctl\nValid Variables: [" + strings.Join(_validArgs, ", ") + "]",
    29  		config.Chinese: "为 wsctl 设置配置字段\n有效变量: [" + strings.Join(_validArgs, ", ") + "]",
    30  	}
    31  )
    32  
    33  // newConfigSetCmd is a command to set config fields from wsctl
    34  func newConfigSetCmd(client client.Client) *cobra.Command {
    35  	return &cobra.Command{
    36  		Use:       client.SelectTranslation(_configSetUse),
    37  		Short:     client.SelectTranslation(_configSetUseCmdShorts),
    38  		Long:      client.SelectTranslation(_configSetUseCmdLong),
    39  		ValidArgs: _validArgs,
    40  		Args: func(cmd *cobra.Command, args []string) error {
    41  			if len(args) != 2 {
    42  				return fmt.Errorf("accepts 2 arg(s), received %d\n"+
    43  					"Valid arg(s): %s", len(args), _validArgs)
    44  			}
    45  			return cobra.OnlyValidArgs(cmd, args[:1])
    46  		},
    47  		RunE: func(cmd *cobra.Command, args []string) error {
    48  			cmd.SilenceUsage = true
    49  			info := info{config: client.Config(), configFile: client.ConfigFilePath()}
    50  			result, err := info.set(args)
    51  			if err != nil {
    52  				return errors.Wrap(err, fmt.Sprintf("problem setting config fields %+v", args))
    53  			}
    54  			cmd.Println(result)
    55  			return nil
    56  		},
    57  	}
    58  }
    59  
    60  // set sets config variable
    61  func (c *info) set(args []string) (string, error) {
    62  	switch args[0] {
    63  	case "endpoint":
    64  		if !isValidEndpoint(args[1]) {
    65  			return "", errors.Errorf("endpoint %s is not valid", args[1])
    66  		}
    67  		c.config.Endpoint = args[1]
    68  	case "language":
    69  		if !isSupportedLanguage(config.Language(args[1])) {
    70  			return "", errors.Errorf("language %s is not supported\nSupported languages: %s",
    71  				args[1], config.SupportedLanguage)
    72  		}
    73  		c.config.Language = config.Language(args[1])
    74  	default:
    75  		return "", ErrConfigNotMatch
    76  	}
    77  
    78  	if err := c.writeConfig(); err != nil {
    79  		return "", err
    80  	}
    81  
    82  	return cases.Title(language.Und).String(args[0]) + " is set to " + args[1], nil
    83  }