github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/alias/aliasremove.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 "fmt" 10 "os" 11 12 "github.com/spf13/cobra" 13 "gopkg.in/yaml.v2" 14 15 "github.com/iotexproject/iotex-core/ioctl/config" 16 "github.com/iotexproject/iotex-core/ioctl/output" 17 "github.com/iotexproject/iotex-core/ioctl/validator" 18 ) 19 20 // Multi-language support 21 var ( 22 _removeCmdShorts = map[config.Language]string{ 23 config.English: "Remove alias", 24 config.Chinese: "移除别名", 25 } 26 _removeCmdUses = map[config.Language]string{ 27 config.English: "remove ALIAS", 28 config.Chinese: "remove 别名", 29 } 30 ) 31 32 // _aliasRemoveCmd represents the alias remove command 33 var _aliasRemoveCmd = &cobra.Command{ 34 Use: config.TranslateInLang(_removeCmdUses, config.UILanguage), 35 Short: config.TranslateInLang(_removeCmdShorts, config.UILanguage), 36 Args: cobra.ExactArgs(1), 37 RunE: func(cmd *cobra.Command, args []string) error { 38 cmd.SilenceUsage = true 39 err := remove(args[0]) 40 return output.PrintError(err) 41 }, 42 } 43 44 // remove removes alias 45 func remove(arg string) error { 46 if err := validator.ValidateAlias(arg); err != nil { 47 return output.NewError(output.ValidationError, "invalid alias", err) 48 } 49 alias := arg 50 delete(config.ReadConfig.Aliases, alias) 51 out, err := yaml.Marshal(&config.ReadConfig) 52 if err != nil { 53 return output.NewError(output.SerializationError, "failed to marshal config", err) 54 } 55 if err := os.WriteFile(config.DefaultConfigFile, out, 0600); err != nil { 56 return output.NewError(output.WriteFileError, 57 fmt.Sprintf("failed to write to config file %s", config.DefaultConfigFile), err) 58 } 59 output.PrintResult(alias + " is removed") 60 return nil 61 }