github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/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"
    16  	"github.com/iotexproject/iotex-core/ioctl/config"
    17  )
    18  
    19  // Multi-language support
    20  var (
    21  	_listShorts = map[config.Language]string{
    22  		config.English: "list all alias",
    23  		config.Chinese: "列出全部别名",
    24  	}
    25  )
    26  
    27  // NewAliasListCmd represents the alias list command
    28  func NewAliasListCmd(c ioctl.Client) *cobra.Command {
    29  	short, _ := c.SelectTranslation(_listShorts)
    30  	return &cobra.Command{
    31  		Use:   "list",
    32  		Short: short,
    33  		Args:  cobra.ExactArgs(0),
    34  		RunE: func(cmd *cobra.Command, args []string) error {
    35  			var keys []string
    36  			for name := range c.Config().Aliases {
    37  				keys = append(keys, name)
    38  			}
    39  			sort.Strings(keys)
    40  			message := aliasListMessage{AliasNumber: len(keys)}
    41  			for _, name := range keys {
    42  				aliasMeta := alias{Address: c.Config().Aliases[name], Name: name}
    43  				message.AliasList = append(message.AliasList, aliasMeta)
    44  			}
    45  			lines := make([]string, 0)
    46  			for _, aliasMeta := range message.AliasList {
    47  				lines = append(lines, fmt.Sprintf("%s - %s", aliasMeta.Address, aliasMeta.Name))
    48  			}
    49  			cmd.Println(fmt.Sprint(strings.Join(lines, "\n")))
    50  			return nil
    51  		},
    52  	}
    53  }
    54  
    55  type aliasListMessage struct {
    56  	AliasNumber int     `json:"aliasNumber"`
    57  	AliasList   []alias `json:"aliasList"`
    58  }