-
Notifications
You must be signed in to change notification settings - Fork 20
/
config_add.go
146 lines (122 loc) · 2.88 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
package cmd
import (
"bufio"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
exo "github.com/exoscale/egoscale/v2"
)
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 := &account.Config{Accounts: []account.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 account.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.Account{*newAccount}
gConfig.Set("defaultAccount", newAccount.Name)
if len(config.Accounts) == 0 {
return nil
}
return saveConfig(filePath, &config)
}
func promptAccountInformation() (*account.Account, error) {
var client *exo.Client
reader := bufio.NewReader(os.Stdin)
account := &account.Account{
Key: "",
Secret: "",
}
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
}
acc, err := readInput(reader, "Account name", account.Account)
if err != nil {
return nil, err
}
if acc != "" {
account.Account = acc
}
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
}
client, err = exo.NewClient(account.Key, account.APISecret())
if err != nil {
return nil, err
}
account.DefaultZone, err = chooseZone(client, nil)
if err != nil {
for {
defaultZone, err := chooseZone(globalstate.EgoscaleClient, allZones)
if err != nil {
return nil, err
}
if defaultZone != "" {
account.DefaultZone = defaultZone
break
}
}
}
return account, nil
}