/
config_attributes.go
198 lines (184 loc) · 4.37 KB
/
config_attributes.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
package config
import (
"fmt"
"os"
"reflect"
"sort"
"strings"
)
var ConfigAttributes = loadAttrs()
// attributes holds meta-representation of Config configuration options
type attributes []ConfigAttribute
func (a attributes) DebugString(cfg *Config) string {
buf := []string{}
attrsUsed := []string{}
envsUsed := []string{}
for _, attr := range a {
if attr.IsZero(cfg) {
continue
}
// Don't include internal fields in debug representation.
if attr.Internal {
continue
}
v := "***"
if !attr.Sensitive {
v = attr.GetString(cfg)
}
attrsUsed = append(attrsUsed, fmt.Sprintf("%s=%s", attr.Name, v))
for _, envName := range attr.EnvVars {
v := os.Getenv(envName)
if v == "" {
continue
}
envsUsed = append(envsUsed, envName)
}
}
if len(attrsUsed) > 0 {
buf = append(buf, fmt.Sprintf("Config: %s", strings.Join(attrsUsed, ", ")))
}
if len(envsUsed) > 0 {
buf = append(buf, fmt.Sprintf("Env: %s", strings.Join(envsUsed, ", ")))
}
return strings.Join(buf, ". ")
}
func (a attributes) Validate(cfg *Config) error {
authsUsed := map[string]bool{}
for _, attr := range a {
if attr.IsZero(cfg) {
continue
}
if !attr.HasAuthAttribute() {
continue
}
authsUsed[attr.Auth] = true
}
if len(authsUsed) <= 1 {
return nil
}
if cfg.AuthType != "" {
// client has auth preference set
return nil
}
names := []string{}
for v := range authsUsed {
names = append(names, v)
}
sort.Strings(names)
return fmt.Errorf("more than one authorization method configured: %s",
strings.Join(names, " and "))
}
// Name implements Loader interface for environment variables
func (a attributes) Name() string {
return "environment"
}
// Configure implements Loader interface for environment variables
func (a attributes) Configure(cfg *Config) error {
for _, attr := range a {
if !attr.IsZero(cfg) {
// don't overwtite a value previously set
continue
}
v, envName := attr.ReadEnv()
if v == "" {
continue
}
err := attr.SetS(cfg, v)
cfg.SetAttrSource(&attr, Source{Type: SourceEnv, Name: envName})
if err != nil {
return err
}
}
return nil
}
func (a attributes) ResolveFromStringMap(cfg *Config, m map[string]string) error {
return a.ResolveFromStringMapWithSource(cfg, m, Source{Type: SourceDynamicConfig})
}
func (a attributes) ResolveFromStringMapWithSource(cfg *Config, m map[string]string, source Source) error {
for _, attr := range a {
if !attr.IsZero(cfg) {
// don't overwtite a value previously set
continue
}
v, ok := m[attr.Name]
if !ok || v == "" {
continue
}
err := attr.SetS(cfg, v)
if err != nil {
return err
}
cfg.SetAttrSource(&attr, source)
}
return nil
}
func (a attributes) ResolveFromAnyMap(cfg *Config, m map[string]interface{}) error {
return a.ResolveFromAnyMapWithSource(cfg, m, Source{Type: SourceDynamicConfig})
}
func (a attributes) ResolveFromAnyMapWithSource(cfg *Config, m map[string]interface{}, source Source) error {
for _, attr := range a {
if !attr.IsZero(cfg) {
// don't overwtite a value previously set
continue
}
v, ok := m[attr.Name]
if !ok {
continue
}
err := attr.Set(cfg, v)
if err != nil {
return err
}
cfg.SetAttrSource(&attr, source)
}
return nil
}
func loadAttrs() (attrs attributes) {
t := reflect.TypeOf(Config{})
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
nameTag := field.Tag.Get("name")
if nameTag == "" {
continue
}
sensitive := false
auth := field.Tag.Get("auth")
authSplit := strings.Split(auth, ",")
if len(authSplit) == 2 {
auth = authSplit[0]
sensitive = authSplit[1] == "sensitive"
}
// internal config fields are skipped in debugging
internal := false
if auth == "-" {
auth = ""
internal = true
}
var authTypes []string
authTypesTag := field.Tag.Get("auth_types")
// If auth_types is not set, use auth as the only auth type
// In this case auth should be set and a valid auth type.
if authTypesTag == "" {
if auth != "" {
authTypes = append(authTypes, auth)
}
} else {
authTypes = strings.Split(authTypesTag, ",")
}
attr := ConfigAttribute{
Name: nameTag,
Auth: auth,
AuthTypes: authTypes,
Kind: field.Type.Kind(),
Sensitive: sensitive,
Internal: internal,
num: i,
}
envTag := field.Tag.Get("env")
if envTag != "" {
attr.EnvVars = strings.Split(envTag, ",")
}
attrs = append(attrs, attr)
}
return
}