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

     1  // Copyright (c) 2019 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  	"encoding/json"
    10  
    11  	"github.com/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  	"gopkg.in/yaml.v2"
    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  	_shorts = map[config.Language]string{
    22  		config.English: "Export aliases to either json or yaml format",
    23  		config.Chinese: "以json或yaml格式导出别名",
    24  	}
    25  	_flagUsages = map[config.Language]string{
    26  		config.English: "set format: json/yaml",
    27  		config.Chinese: "设置格式:json / yaml",
    28  	}
    29  	_invalidFlag = map[config.Language]string{
    30  		config.English: "invalid flag %s",
    31  		config.Chinese: "不可用的flag参数 %s",
    32  	}
    33  )
    34  
    35  // NewAliasExport represents the alias export command
    36  func NewAliasExport(c ioctl.Client) *cobra.Command {
    37  	var format string
    38  	short, _ := c.SelectTranslation(_shorts)
    39  	flagUsage, _ := c.SelectTranslation(_flagUsages)
    40  	_invalidFlag, _ := c.SelectTranslation(_invalidFlag)
    41  	ec := &cobra.Command{
    42  		Use:   "export",
    43  		Short: short,
    44  		Args:  cobra.ExactArgs(0),
    45  		RunE: func(cmd *cobra.Command, args []string) error {
    46  
    47  			cmd.SilenceUsage = true
    48  			exportAliases := aliases{}
    49  			for name, address := range c.Config().Aliases {
    50  				exportAliases.Aliases = append(exportAliases.Aliases, alias{Name: name, Address: address})
    51  			}
    52  
    53  			switch format {
    54  			case "json":
    55  				output, err := json.Marshal(exportAliases)
    56  				if err != nil {
    57  					return nil
    58  				}
    59  				cmd.Println(string(output))
    60  				return nil
    61  			case "yaml":
    62  				output, err := yaml.Marshal(exportAliases)
    63  				if err != nil {
    64  					return nil
    65  				}
    66  				cmd.Println(string(output))
    67  				return nil
    68  			default:
    69  				return errors.Errorf(_invalidFlag, format)
    70  			}
    71  		},
    72  	}
    73  	ec.Flags().StringVarP(&format,
    74  		"format", "f", "json", flagUsage)
    75  
    76  	return ec
    77  }
    78  
    79  type aliases struct {
    80  	Aliases []alias `json:"aliases" yaml:"aliases"`
    81  }
    82  
    83  type alias struct {
    84  	Name    string `json:"name" yaml:"name"`
    85  	Address string `json:"address" yaml:"address"`
    86  }