github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/alias/aliaslist.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 "sort" 11 "strings" 12 13 "github.com/spf13/cobra" 14 15 "github.com/iotexproject/iotex-core/ioctl/config" 16 "github.com/iotexproject/iotex-core/ioctl/output" 17 ) 18 19 // Multi-language support 20 var ( 21 _listCmdShorts = map[config.Language]string{ 22 config.English: "List aliases", 23 config.Chinese: "εεΊε«ε", 24 } 25 ) 26 27 // _aliasListCmd represents the alias list command 28 var _aliasListCmd = &cobra.Command{ 29 Use: "list", 30 Short: config.TranslateInLang(_listCmdShorts, config.UILanguage), 31 Args: cobra.ExactArgs(0), 32 Run: func(cmd *cobra.Command, args []string) { 33 aliasList() 34 }, 35 } 36 37 type aliasListMessage struct { 38 AliasNumber int `json:"aliasNumber"` 39 AliasList []alias `json:"aliasList"` 40 } 41 42 func aliasList() { 43 var keys []string 44 for name := range config.ReadConfig.Aliases { 45 keys = append(keys, name) 46 } 47 sort.Strings(keys) 48 message := aliasListMessage{AliasNumber: len(keys)} 49 for _, name := range keys { 50 aliasMeta := alias{Address: config.ReadConfig.Aliases[name], Name: name} 51 message.AliasList = append(message.AliasList, aliasMeta) 52 } 53 fmt.Println(message.String()) 54 } 55 56 func (m *aliasListMessage) String() string { 57 if output.Format == "" { 58 lines := make([]string, 0) 59 for _, aliasMeta := range m.AliasList { 60 lines = append(lines, fmt.Sprintf("%s - %s", aliasMeta.Address, aliasMeta.Name)) 61 } 62 return fmt.Sprint(strings.Join(lines, "\n")) 63 } 64 return output.FormatString(output.Result, m) 65 }