-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
139 lines (126 loc) · 3.68 KB
/
config.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
)
// ClientConfig is the basic configuration settings required by all clients.
type ClientConfig struct {
Account int // RightScale account ID
LoginHost string // RightScale API login host, e.g. "us-3.rightscale.com"
Email string // RightScale API login email
Password string // RightScale API login password
RefreshToken string // RightScale API refresh token
FlexeraOne bool
}
// LoadConfig loads the client configuration from disk
func LoadConfig(path string) (*ClientConfig, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var config ClientConfig
err = json.Unmarshal(content, &config)
if err != nil {
return nil, err
}
config.Password, err = Decrypt(config.Password)
if err != nil {
return nil, err
}
config.RefreshToken, err = Decrypt(config.RefreshToken)
return &config, err
}
// Save config encrypts the password and/or refresh token;
// persists the config to file
func (cfg *ClientConfig) Save(path string) error {
encrypted_password, err := Encrypt(cfg.Password)
if err != nil {
return fmt.Errorf("Failed to encrypt password: %s", err)
}
cfg.Password = encrypted_password
encrypted_refresh, err := Encrypt(cfg.RefreshToken)
if err != nil {
return fmt.Errorf("Failed to encrypt refresh token: %s", err)
}
cfg.RefreshToken = encrypted_refresh
bytes, err := json.Marshal(cfg)
if err != nil {
return fmt.Errorf("Failed to serialize config: %s", err)
}
err = ioutil.WriteFile(path, bytes, 0644)
if err != nil {
return fmt.Errorf("Failed to write config file: %s", err)
}
return nil
}
// CreateConfig creates a configuration file and saves it to the file at the given path.
func CreateConfig(path string) error {
config, _ := LoadConfig(path)
var emailDef, passwordDef, accountDef, hostDef, refreshTokenDef string
var flexeraOneDef bool
if config != nil {
yn := PromptConfirmation("Found existing configuration file %v, overwrite? (y/N): ", path)
if yn != "y" {
PrintSuccess("Exiting")
return nil
}
emailDef = fmt.Sprintf(" (%v)", config.Email)
accountDef = fmt.Sprintf(" (%v)", config.Account)
passwordDef = " (leave blank to leave unchanged)"
if config.LoginHost == "" {
config.LoginHost = "my.rightscale.com"
}
hostDef = fmt.Sprintf(" (%v)", config.LoginHost)
refreshTokenDef = " (leave blank to leave unchanged)"
flexeraOneDef = config.FlexeraOne
} else {
config = &ClientConfig{}
}
fmt.Fprintf(out, "Account ID%v: ", accountDef)
var newAccount string
fmt.Fscanln(in, &newAccount)
if newAccount != "" {
a, err := strconv.Atoi(newAccount)
if err != nil {
return fmt.Errorf("Account ID must be an integer, got '%s'.", newAccount)
}
config.Account = a
}
fmt.Fprintf(out, "Login email%v: ", emailDef)
var newEmail string
fmt.Fscanln(in, &newEmail)
if newEmail != "" {
config.Email = newEmail
}
fmt.Fprintf(out, "Login password%v: ", passwordDef)
var newPassword string
fmt.Fscanln(in, &newPassword)
if newPassword != "" {
config.Password = newPassword
}
fmt.Fprintf(out, "API Login host%v: ", hostDef)
var newLoginHost string
fmt.Fscanln(in, &newLoginHost)
if newLoginHost != "" {
config.LoginHost = newLoginHost
}
fmt.Fprintf(out, "API Refresh Token%v: ", refreshTokenDef)
var newRefreshToken string
fmt.Fscanln(in, &newRefreshToken)
if newRefreshToken != "" {
config.RefreshToken = newRefreshToken
}
fmt.Fprintf(out, "FlexeraOne Enabled %v: ", flexeraOneDef)
var newFlexeraOne bool
fmt.Fscanln(in, &newFlexeraOne)
if newFlexeraOne {
config.FlexeraOne = newFlexeraOne
}
err := config.Save(path)
if err != nil {
return fmt.Errorf("Failed to save config: %s", err)
}
return nil
}