-
Notifications
You must be signed in to change notification settings - Fork 322
/
aliasexport.go
97 lines (82 loc) · 2.48 KB
/
aliasexport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package alias
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/config"
)
// Multi-language support
var (
shorts = map[config.Language]string{
config.English: "Export aliases to either json or yaml format",
config.Chinese: "以json或yaml格式导出别名",
}
uses = map[config.Language]string{
config.English: "export",
config.Chinese: "export",
}
flagUsages = map[config.Language]string{
config.English: "set format: json/yaml",
config.Chinese: "设置格式:json / yaml",
}
invalidFlag = map[config.Language]string{
config.English: "invalid flag %s",
config.Chinese: "不可用的flag参数 %s",
}
)
// NewAliasExport represents the alias export command
func NewAliasExport(c ioctl.Client) *cobra.Command {
var format string
use, _ := c.SelectTranslation(uses)
short, _ := c.SelectTranslation(shorts)
flagUsage, _ := c.SelectTranslation(flagUsages)
invalidFlag, _ := c.SelectTranslation(invalidFlag)
ec := &cobra.Command{
Use: use,
Short: short,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
exportAliases := aliases{}
for name, address := range config.ReadConfig.Aliases {
exportAliases.Aliases = append(exportAliases.Aliases, alias{Name: name, Address: address})
}
switch format {
default:
cmd.SilenceUsage = false
return fmt.Errorf(invalidFlag, format)
case "json":
output, err := json.Marshal(exportAliases)
if err != nil {
return nil
}
println(string(output))
return nil
case "yaml":
output, err := yaml.Marshal(exportAliases)
if err != nil {
return nil
}
println(string(output))
return nil
}
},
}
ec.Flags().StringVarP(&format,
"format", "f", "json", flagUsage)
return ec
}
type aliases struct {
Aliases []alias `json:"aliases" yaml:"aliases"`
}
type alias struct {
Name string `json:"name" yaml:"name"`
Address string `json:"address" yaml:"address"`
}