github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/alias/aliasset.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package alias
     7  
     8  import (
     9  	"github.com/pkg/errors"
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/iotexproject/iotex-core/ioctl"
    13  	"github.com/iotexproject/iotex-core/ioctl/config"
    14  	"github.com/iotexproject/iotex-core/ioctl/validator"
    15  )
    16  
    17  // Multi-language support
    18  var (
    19  	_setCmdShorts = map[config.Language]string{
    20  		config.English: "Set alias for address",
    21  		config.Chinese: "设定地址的别名",
    22  	}
    23  	_setCmdUses = map[config.Language]string{
    24  		config.English: "set ALIAS ADDRESS",
    25  		config.Chinese: "set 别名 地址",
    26  	}
    27  )
    28  
    29  // NewAliasSetCmd represents the alias set command
    30  func NewAliasSetCmd(c ioctl.Client) *cobra.Command {
    31  	use, _ := c.SelectTranslation(_setCmdUses)
    32  	short, _ := c.SelectTranslation(_setCmdShorts)
    33  
    34  	return &cobra.Command{
    35  		Use:   use,
    36  		Short: short,
    37  		Args:  cobra.ExactArgs(2),
    38  		RunE: func(cmd *cobra.Command, args []string) error {
    39  			cmd.SilenceUsage = true
    40  			if err := validator.ValidateAlias(args[0]); err != nil {
    41  				return errors.Wrap(err, "invalid alias")
    42  			}
    43  			if err := validator.ValidateAddress(args[1]); err != nil {
    44  				return errors.Wrap(err, "invalid address")
    45  			}
    46  			if err := c.SetAliasAndSave(args[0], args[1]); err != nil {
    47  				return errors.Wrap(err, "failed to write to config file ")
    48  			}
    49  			cmd.Println(args[0] + " has been set!")
    50  			return nil
    51  		},
    52  	}
    53  }