-
Notifications
You must be signed in to change notification settings - Fork 18
/
config_add.go
188 lines (156 loc) · 3.77 KB
/
config_add.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cmd
import (
"bufio"
"fmt"
"os"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
func init() {
configCmd.AddCommand(&cobra.Command{
Use: "add",
Short: "Add a new account to configuration",
RunE: func(cmd *cobra.Command, args []string) error {
newAccount, err := promptAccountInformation()
if err != nil {
return err
}
config := &config{Accounts: []account{*newAccount}}
if askQuestion("Set [" + newAccount.Name + "] as default account?") {
config.DefaultAccount = newAccount.Name
gConfig.Set("defaultAccount", newAccount.Name)
}
return saveConfig(gConfig.ConfigFileUsed(), config)
},
})
}
func addConfigAccount(firstRun bool) error {
var (
config config
err error
)
filePath := gConfig.ConfigFileUsed()
if firstRun {
if filePath, err = createConfigFile(defaultConfigFileName); err != nil {
return err
}
gConfig.SetConfigFile(filePath)
}
newAccount, err := promptAccountInformation()
if err != nil {
return err
}
config.DefaultAccount = newAccount.Name
config.Accounts = []account{*newAccount}
gConfig.Set("defaultAccount", newAccount.Name)
if len(config.Accounts) == 0 {
return nil
}
return saveConfig(filePath, &config)
}
func promptAccountInformation() (*account, error) {
var client *egoscale.Client
reader := bufio.NewReader(os.Stdin)
account := &account{
Endpoint: defaultEndpoint,
Key: "",
Secret: "",
}
for i := 0; ; i++ {
if i > 0 {
endpoint, err := readInput(reader, "API Endpoint", account.Endpoint)
if err != nil {
return nil, err
}
if endpoint != account.Endpoint {
account.Endpoint = endpoint
}
}
apiKey, err := readInput(reader, "API Key", account.Key)
if err != nil {
return nil, err
}
if apiKey != account.Key {
account.Key = apiKey
}
secret := account.APISecret()
secretShow := account.APISecret()
if secret != "" && len(secret) > 10 {
secretShow = secret[0:7] + "..."
}
secretKey, err := readInput(reader, "Secret Key", secretShow)
if err != nil {
return nil, err
}
if secretKey != secret && secretKey != secretShow {
account.Secret = secretKey
}
client = egoscale.NewClient(account.Endpoint, account.Key, account.APISecret())
fmt.Printf("Retrieving account information...")
resp, err := client.GetWithContext(gContext, egoscale.Account{})
if err != nil {
if egoerr, ok := err.(*egoscale.ErrorResponse); ok && egoerr.ErrorCode == egoscale.ErrorCode(403) {
fmt.Print(`
Unable to retrieve information, please enter your account details:
`)
for {
acc, err := readInput(reader, "Account name", account.Account)
if err != nil {
return nil, err
}
if acc != "" {
account.Account = acc
break
}
}
break
}
fmt.Print(` failure.
Let's start over.
`)
} else {
fmt.Print(" done!\n\n")
acc := resp.(*egoscale.Account)
account.Name = acc.Name
account.Account = acc.Name
break
}
}
name, err := readInput(reader, "Name", account.Name)
if err != nil {
return nil, err
}
if name != "" {
account.Name = name
}
for {
if a := getAccountByName(account.Name); a == nil {
break
}
fmt.Printf("Name [%s] already exist\n", name)
name, err = readInput(reader, "Name", account.Name)
if err != nil {
return nil, err
}
account.Name = name
}
account.DefaultZone, err = chooseZone(client, nil)
if err != nil {
if egoerr, ok := err.(*egoscale.ErrorResponse); ok && egoerr.ErrorCode == egoscale.ErrorCode(403) {
for {
defaultZone, err := chooseZone(cs, allZones)
if err != nil {
return nil, err
}
if defaultZone != "" {
account.DefaultZone = defaultZone
break
}
}
} else {
return nil, err
}
}
account.DNSEndpoint = buildDNSAPIEndpoint(account.Endpoint)
return account, nil
}