-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolsconfig.go
316 lines (290 loc) · 10.5 KB
/
toolsconfig.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package toolsconfig
import (
"fmt"
"github.com/spf13/viper"
)
const (
ConfigFormat = "yaml"
ConfigFilePermissions = 0600
)
type Configuration interface {
// SetAzureSubscriptionCredentials set the azure subscription credentials.
SetAzureSubscriptionCredentials(entry AzureSubscriptionCredential) error
// GetAzureSubscriptionCredentials get the azure subscription credentials.
GetAzureSubscriptionCredentials(nameOrID string) (*AzureSubscriptionCredential, error)
// GetAllAzureSubscriptionCredentials returns all azure subscription credentials available in config file.
GetAllAzureSubscriptionCredentials() []AzureSubscriptionCredential
// SetServerCredentials set the server credentials.
SetServerCredentials(entry ServerCredential) error
// GetServerCredentials get the server credentials.
GetServerCredentials(url string) (*ServerCredential, error)
// GetAllServerCredentials returns all generic credentials available in config file.
GetAllServerCredentials() []ServerCredential
// SetGenericCredentials set the generic credentials.
SetGenericCredentials(entry GenericCredential) error
// GetGenericCredentials set the generic credentials.
GetGenericCredentials(key string) (*GenericCredential, error)
// GetAllGenericCredentials returns all generic credentials available in config file.
GetAllGenericCredentials() []GenericCredential
// GetGeneric ...
GetGeneric(key string) string
// SetDefaultSubscription set the default azure subscription.
SetDefaultSubscription(subscriptionName string) error
// SaveFavourite saves a favourite in the config file.
SaveFavourite(tool, name string, args []string) error
// GetFavourite get a favourite entry identified by the toolname and it's name.
GetFavourite(tool, name string) (*Favourite, error)
// GetFavourites get all favourites for a given tool.
GetFavourites(tool string) []Favourite
// RemoveFavourite remove a favourite from the config file.
RemoveFavourite(tool, name string) error
}
var (
configDirectory *string = nil
configFile *string = nil
)
func ConfigFileLocation(dir, filename string) {
configDirectory = &dir
configFile = &filename
}
// NewToolConfiguration creates a new configuration object.
// You have to set the config file location before calling this function by calling `toolconfig.ConfigFileLocation("dir", "filename")`.
// Use the options
// * RequiredServer(..)
// * RequiredAzureSubscription(..)
// * RequiredGeneric(..)
// to specify which credentials are required. If the credentials are not available in the configuration,
// an error is returned immediately.
var NewToolConfiguration = func(options ...ConfigOption) (Configuration, error) {
if configDirectory == nil || configFile == nil {
return nil, fmt.Errorf(`configuration file location not set (Call toolconfig.ConfigFileLocation("dir", "filename"))`)
}
opts := ConfigOptions{
updateConfig: true,
configDirectory: *configDirectory,
configFile: *configFile,
}
for _, option := range options {
option(&opts)
}
c := &ToolConfiguration{
servers: map[string]*ServerCredential{},
azureSubscriptions: map[string]*AzureSubscriptionCredential{},
generics: map[string]*GenericCredential{},
}
file, err := configFileName(opts.configDirectory, opts.configFile)
if err != nil {
return nil, wrapErr(err)
}
viper.SetConfigType(ConfigFormat)
viper.SetConfigFile(*file)
viper.SetConfigPermissions(ConfigFilePermissions)
c.config = readConfiguration()
err = verifyRequiredValues(c, opts)
if err != nil {
dirty := c.config.merge(opts.requiredConfig())
if dirty && opts.updateConfig {
err := saveConfiguration(c.config)
if err != nil {
return nil, wrapErr(err)
}
}
return nil, err
}
return c, err
}
func verifyRequiredValues(c Configuration, opts ConfigOptions) error {
var missingCredentials []string
for _, serverURL := range opts.requiredServers {
serverCredential, err := c.GetServerCredentials(serverURL)
if err != nil || !serverCredential.valid() {
missingCredentials = append(missingCredentials, fmt.Sprintf("Server: %s", serverURL))
}
}
for _, idOrName := range opts.requiredAzureSubscriptions {
subscriptionCredential, err := c.GetAzureSubscriptionCredentials(idOrName)
if err != nil || !subscriptionCredential.valid() {
missingCredentials = append(missingCredentials, fmt.Sprintf("AzureSubscriptionCredential: %s", idOrName))
}
}
for _, key := range opts.requiredGenerics {
generic, err := c.GetGenericCredentials(key)
if err != nil || !generic.valid() {
missingCredentials = append(missingCredentials, fmt.Sprintf("GenericCredential: %s", key))
}
}
if len(missingCredentials) > 0 {
return wrapErr(fmt.Errorf("missing entries"), missingCredentials...)
}
return nil
}
func (c *ToolConfiguration) SetAzureSubscriptionCredentials(entry AzureSubscriptionCredential) error {
if entry.Name == "" {
return fmt.Errorf("subscription name missing")
}
_, index, err := c.config.azureSubscriptionCredential(entry.Name)
if err != nil {
c.config.AzureSubscriptions = append(c.config.AzureSubscriptions, entry)
} else {
c.config.AzureSubscriptions[*index].SubscriptionID = entry.SubscriptionID
c.config.AzureSubscriptions[*index].TenantID = entry.TenantID
c.config.AzureSubscriptions[*index].ClientID = entry.ClientID
c.config.AzureSubscriptions[*index].ClientSecret = entry.ClientSecret
}
return saveConfiguration(c.config)
}
func (c *ToolConfiguration) SetServerCredentials(entry ServerCredential) error {
if entry.URL == "" {
return fmt.Errorf("server url missing")
}
_, index, err := c.config.serverCredential(entry.URL)
if err != nil {
c.config.Servers = append(c.config.Servers, entry)
} else {
c.config.Servers[*index].URL = entry.URL
c.config.Servers[*index].Username = entry.Username
c.config.Servers[*index].Password = entry.Password
}
return saveConfiguration(c.config)
}
func (c *ToolConfiguration) SetGenericCredentials(entry GenericCredential) error {
if entry.Key == "" {
return fmt.Errorf("generic credential key missing")
}
_, index, err := c.config.genericCredential(entry.Key)
if err != nil {
c.config.Generic = append(c.config.Generic, entry)
} else {
c.config.Generic[*index].Key = entry.Key
c.config.Generic[*index].Value = entry.Value
}
return saveConfiguration(c.config)
}
// GetServerCredentials find the credentials for the given url. Returns errNotFound if not found.
func (c *ToolConfiguration) GetServerCredentials(url string) (*ServerCredential, error) {
if fromEnv := (ServerCredential{}.FromEnv(url)); fromEnv != nil {
return fromEnv, nil
}
if serverCred, ok := c.servers[url]; ok {
return serverCred, nil
}
credential, _, err := c.config.serverCredential(url)
if err != nil {
return nil, err
}
c.servers[url] = credential
return credential, nil
}
func (c *ToolConfiguration) GetAllServerCredentials() []ServerCredential {
return c.config.Servers
}
// GetAzureSubscriptionCredentials find the credentials for the given name or subscription id. Returns errNotFound if not found.
func (c *ToolConfiguration) GetAzureSubscriptionCredentials(nameOrID string) (*AzureSubscriptionCredential, error) {
if fromEnv := (AzureSubscriptionCredential{}.FromEnv(nameOrID)); fromEnv != nil {
return fromEnv, nil
}
searchNameOrId := nameOrID
if nameOrID == "" && c.config.DefaultAzureSubscription != "" {
searchNameOrId = c.config.DefaultAzureSubscription
}
if azureCred, ok := c.azureSubscriptions[searchNameOrId]; ok {
return azureCred, nil
}
credential, _, err := c.config.azureSubscriptionCredential(searchNameOrId)
if err != nil {
return nil, err
}
c.azureSubscriptions[searchNameOrId] = credential
return credential, nil
}
func (c *ToolConfiguration) GetAllAzureSubscriptionCredentials() []AzureSubscriptionCredential {
return c.config.AzureSubscriptions
}
// GetGenericCredentials find the credentials for the given key. Returns errNotFound if not found.
func (c *ToolConfiguration) GetGenericCredentials(key string) (*GenericCredential, error) {
if fromEnv := (GenericCredential{}.FromEnv(key)); fromEnv != nil {
return fromEnv, nil
}
if genericCred, ok := c.generics[key]; ok {
return genericCred, nil
}
credential, _, err := c.config.genericCredential(key)
if err != nil {
return nil, err
}
c.generics[key] = credential
return credential, nil
}
func (c *ToolConfiguration) GetAllGenericCredentials() []GenericCredential {
return c.config.Generic
}
// GetGeneric is a simple call to get only the value of a generic key. Empty string if not exists.
func (c *ToolConfiguration) GetGeneric(key string) string {
credentials, err := c.GetGenericCredentials(key)
if err != nil {
return ""
}
return credentials.Value
}
// SetDefaultSubscription updates the default subscription value in the configuration. GetAzureSubscriptionCredentials returns the
// subscription credentials with this name or id if the given identifier is empty.
func (c *ToolConfiguration) SetDefaultSubscription(subscriptionName string) error {
_, _, err := c.config.azureSubscriptionCredential(subscriptionName)
if err != nil {
return wrapErr(err, "subscription does not exist")
}
c.config.DefaultAzureSubscription = subscriptionName
err = saveConfiguration(c.config)
if err != nil {
return wrapErr(err)
}
return nil
}
func (c *ToolConfiguration) SaveFavourite(tool, name string, args []string) error {
if c.config.Favourites == nil {
c.config.Favourites = make(map[string]map[string]Favourite, 1)
}
if c.config.Favourites[tool] == nil {
c.config.Favourites[tool] = make(map[string]Favourite, 1)
}
c.config.Favourites[tool][name] = Favourite{Name: name, Args: args}
err := saveConfiguration(c.config)
if err != nil {
return wrapErr(err)
}
return nil
}
func (c *ToolConfiguration) GetFavourite(tool, name string) (*Favourite, error) {
if tool, ok := c.config.Favourites[tool]; ok {
if command, ok := tool[name]; ok {
return &command, nil
}
}
return nil, wrapErr(errNotFound)
}
func (c *ToolConfiguration) GetFavourites(tool string) []Favourite {
if tool, ok := c.config.Favourites[tool]; ok {
result := make([]Favourite, len(tool))
idx := 0
for _, favourite := range tool {
result[idx] = favourite
idx++
}
return result
}
return []Favourite{}
}
func (c *ToolConfiguration) RemoveFavourite(tool, name string) error {
if c.config.Favourites == nil {
return wrapErr(fmt.Errorf("no saved favourites"))
}
if c.config.Favourites[tool] == nil {
return wrapErr(fmt.Errorf("no favourites exist for tool %s", tool))
}
delete(c.config.Favourites[tool], name)
err := saveConfiguration(c.config)
if err != nil {
return wrapErr(err)
}
return nil
}