-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
161 lines (143 loc) · 4.38 KB
/
model.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
package toolsconfig
import (
"fmt"
"os"
"strings"
)
var _ Configuration = &ToolConfiguration{}
type ToolConfiguration struct {
config *Config
servers map[string]*ServerCredential
azureSubscriptions map[string]*AzureSubscriptionCredential
generics map[string]*GenericCredential
configReader func() (*Configuration, error)
}
type Config struct {
DefaultAzureSubscription string `yaml:"defaultAzureSubscription,omitempty"`
Servers []ServerCredential `yaml:"servers"`
AzureSubscriptions []AzureSubscriptionCredential `yaml:"azureSubscriptions"`
Generic []GenericCredential `yaml:"generics"`
Favourites map[string]map[string]Favourite `yaml:"favourites"`
}
type ServerCredential struct {
URL string `yaml:"url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type AzureSubscriptionCredential struct {
Name string `yaml:"name"`
SubscriptionID string `yaml:"subscriptionID"`
TenantID string `yaml:"tenantID"`
ClientID string `yaml:"clientID"`
ClientSecret string `yaml:"clientSecret"`
}
type GenericCredential struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
}
type Favourite struct {
Name string `yaml:"name"`
Args []string `yaml:"args,flow"`
}
func (s Favourite) String() string {
return fmt.Sprintf("%s - '%s'", s.Name, strings.Join(s.Args, " "))
}
func (c *Config) merge(required *Config) bool {
var dirty bool
for _, server := range required.Servers {
_, _, err := c.serverCredential(server.URL)
if err != nil {
c.Servers = append(c.Servers, server)
dirty = true
}
}
for _, subscription := range required.AzureSubscriptions {
_, _, err := c.azureSubscriptionCredential(subscription.SubscriptionID)
if err != nil {
c.AzureSubscriptions = append(c.AzureSubscriptions, subscription)
dirty = true
}
}
for _, generic := range required.Generic {
_, _, err := c.genericCredential(generic.Key)
if err != nil {
c.Generic = append(c.Generic, generic)
dirty = true
}
}
return dirty
}
func (c Config) serverCredential(url string) (*ServerCredential, *int, error) {
for index, server := range c.Servers {
if server.URL == url {
return &server, &index, nil
}
}
return nil, nil, wrapErr(errNotFound, "server '"+url+"'")
}
func (c Config) azureSubscriptionCredential(nameOrID string) (*AzureSubscriptionCredential, *int, error) {
for index, subscription := range c.AzureSubscriptions {
if subscription.Name == nameOrID || subscription.SubscriptionID == nameOrID {
return &subscription, &index, nil
}
}
return nil, nil, wrapErr(errNotFound, "subscription '"+nameOrID+"'")
}
func (c Config) genericCredential(key string) (*GenericCredential, *int, error) {
for index, generic := range c.Generic {
if generic.Key == key {
return &generic, &index, nil
}
}
return nil, nil, wrapErr(errNotFound, "generic '"+key+"'")
}
func (c ServerCredential) valid() bool {
return c.Username != "" && c.Password != ""
}
func (c ServerCredential) FromEnv(url string) *ServerCredential {
username := os.Getenv(toEnvironmentKey(url, "username"))
password := os.Getenv(toEnvironmentKey(url, "password"))
result := ServerCredential{
URL: url,
Username: username,
Password: password,
}
if result.valid() {
return &result
}
return nil
}
func (c AzureSubscriptionCredential) valid() bool {
return c.SubscriptionID != "" && c.TenantID != "" && c.ClientID != "" && c.ClientSecret != ""
}
func (c AzureSubscriptionCredential) FromEnv(name string) *AzureSubscriptionCredential {
subscriptionId := os.Getenv(toEnvironmentKey(name, "subscriptionId"))
tenantId := os.Getenv(toEnvironmentKey(name, "tenantId"))
clientId := os.Getenv(toEnvironmentKey(name, "clientId"))
clientSecret := os.Getenv(toEnvironmentKey(name, "clientSecret"))
result := AzureSubscriptionCredential{
Name: name,
SubscriptionID: subscriptionId,
TenantID: tenantId,
ClientID: clientId,
ClientSecret: clientSecret,
}
if result.valid() {
return &result
}
return nil
}
func (c GenericCredential) valid() bool {
return c.Value != ""
}
func (c GenericCredential) FromEnv(key string) *GenericCredential {
value := os.Getenv(toEnvironmentKey(key, "value"))
result := GenericCredential{
Key: key,
Value: value,
}
if result.valid() {
return &result
}
return nil
}