-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
138 lines (125 loc) · 3.96 KB
/
init.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package commands
import (
"encoding/json"
"fmt"
"github.com/cnrancher/tcr-access-control/pkg/cmdconfig"
"github.com/cnrancher/tcr-access-control/pkg/config"
"github.com/cnrancher/tcr-access-control/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type initCmd struct {
baseCmd
}
func newInitCmd() *initCmd {
cc := &initCmd{}
cc.baseCmd.cmd = &cobra.Command{
Use: "init",
Short: "Setup Tencent Cloud Config",
Example: ` tcr-access-control init \
--registryID=<tcr-instance-id> \
--language=zh-CN \
--region=ap-guangzhou \
--secretID=xxx \
--secretKey=xxx`,
RunE: func(cmd *cobra.Command, args []string) error {
initializeFlagsConfig(cmd, cmdconfig.DefaultProvider)
logrus.Debugf("Debug output enabled")
if cmdconfig.GetString("config") == "" {
return fmt.Errorf("config file not specified")
}
logrus.Debugf("config file: %v", cmdconfig.GetString("config"))
cfg := config.Config{}
if cmdconfig.GetString("secretID") != "" &&
cmdconfig.GetString("secretKey") != "" &&
cmdconfig.GetString("registryID") != "" {
// Get config from command-line parameter
cfg = config.Config{
Language: cmdconfig.GetString("language"),
Region: cmdconfig.GetString("region"),
SecretID: cmdconfig.GetString("secretID"),
SecretKey: cmdconfig.GetString("secretKey"),
}
var err error
cfg.SecretID, err = utils.EncryptAES(
utils.AesEncryptKey, cfg.SecretID)
if err != nil {
return fmt.Errorf("Failed to encrypt secretID: %v", err)
}
cfg.SecretKey, err = utils.EncryptAES(
utils.AesEncryptKey, cfg.SecretKey)
if err != nil {
return fmt.Errorf("Failed to encrypt secretKey: %v", err)
}
cfg.RegistryID = cmdconfig.GetString("registryID")
} else {
// Get config from user input
logrus.Infof("Start init config:")
fmt.Printf(
"Default language (zh-CN/en-US) (default: en-US): ")
fmt.Scanf("%s", &cfg.Language)
switch cfg.Language {
case "zh-CN":
case "en-US":
case "":
logrus.Infof("Set language to default 'en-US'")
cfg.Language = "en-US"
default:
logrus.Errorf(
"Invalid language [%s], set to 'en-US'", cfg.Language)
cfg.Language = "en-US"
}
fmt.Printf("Region (default: ap-guangzhou): ")
fmt.Scanf("%s", &cfg.Region)
if cfg.Region == "" {
logrus.Infof("Set region to default 'ap-guangzhou'")
cfg.Region = "ap-guangzhou"
}
fmt.Printf("SecretID: ")
fmt.Scanf("%s", &cfg.SecretID)
if cfg.SecretID == "" {
return fmt.Errorf("secretID should not be empty")
}
var err error
cfg.SecretID, err = utils.EncryptAES(
utils.AesEncryptKey, cfg.SecretID)
if err != nil {
return fmt.Errorf("Failed to encrypt secretID: %v", err)
}
fmt.Printf("SecretKey: ")
fmt.Scanf("%s", &cfg.SecretKey)
if cfg.SecretKey == "" {
return fmt.Errorf("secretKey should not be empty")
}
cfg.SecretKey, err = utils.EncryptAES(
utils.AesEncryptKey, cfg.SecretKey)
if err != nil {
return fmt.Errorf("Failed to encrypt secretKey: %v", err)
}
fmt.Printf("TCR Instance ID: ")
fmt.Scanf("%s", &cfg.RegistryID)
if cfg.RegistryID == "" {
return fmt.Errorf("TCR Instance ID should not be empty")
}
}
b, _ := json.MarshalIndent(cfg, "", " ")
logrus.Debugf("config: %v", string(b))
err := config.SaveConfig(&cfg, cmdconfig.GetString("config"))
if err != nil {
return err
}
logrus.Infof("Saved config to %q", cmdconfig.GetString("config"))
return nil
},
}
cc.cmd.Flags().StringP("language", "l", "en-US", "Laguage")
cc.cmd.Flags().StringP("region", "r", "ap-guangzhou", "Region")
cc.cmd.Flags().StringP("secretID", "", "", "secretID")
cc.cmd.Flags().StringP("secretKey", "", "", "secretKey")
cc.cmd.Flags().StringP("registryID", "", "", "registryID")
cc.cmd.Flags().StringP("config", "", utils.TAC_CONFIG_FILE_HOME, "config file")
return cc
}
func (cc *initCmd) getCommand() *cobra.Command {
return cc.cmd
}