-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathenvironment.go
More file actions
374 lines (319 loc) · 9.22 KB
/
environment.go
File metadata and controls
374 lines (319 loc) · 9.22 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Package env provides utilities for dealing with environment variables.
//
// It is intended for internal use by buildkite-agent only.
package env
import (
"encoding/json"
"runtime"
"sort"
"strconv"
"strings"
"github.com/puzpuzpuz/xsync/v2"
)
// Environment is a map of environment variables, with the keys normalized
// for case-insensitive operating systems
type Environment struct {
underlying *xsync.MapOf[string, string]
}
func New() *Environment {
return &Environment{underlying: xsync.NewMapOf[string]()}
}
func NewWithLength(length int) *Environment {
return &Environment{underlying: xsync.NewMapOfPresized[string](length)}
}
func FromMap(m map[string]string) *Environment {
env := &Environment{underlying: xsync.NewMapOfPresized[string](len(m))}
for k, v := range m {
env.Set(k, v)
}
return env
}
// Split splits an environment variable (in the form "name=value") into the name
// and value substrings. If there is no '=', or the first '=' is at the start,
// it returns `"", "", false`.
func Split(l string) (name, value string, ok bool) {
// Variable names should not contain '=' on any platform...and yet Windows
// creates environment variables beginning with '=' in some circumstances.
// See https://github.com/golang/go/issues/49886.
// Dropping them matches the previous behaviour on Windows, which used SET
// to obtain the state of environment variables.
i := strings.IndexRune(l, '=')
// Either there is no '=', or it is at the start of the string.
// Both are disallowed.
if i <= 0 {
return "", "", false
}
return l[:i], l[i+1:], true
}
// FromSlice creates a new environment from a string slice of KEY=VALUE
func FromSlice(s []string) *Environment {
env := NewWithLength(len(s))
for _, l := range s {
if k, v, ok := Split(l); ok {
env.Set(k, v)
}
}
return env
}
// Pair is an environment variable name/value pair.
type Pair struct{ Name, Value string }
// DumpPairs returns a copy of the environment with all keys normalised.
func (e *Environment) DumpPairs() []Pair {
d := make([]Pair, 0, e.underlying.Size())
e.underlying.Range(func(k, v string) bool {
d = append(d, Pair{
Name: normalizeKeyName(k),
Value: v,
})
return true
})
return d
}
// Dump returns a copy of the environment with all keys normalized
func (e *Environment) Dump() map[string]string {
d := make(map[string]string, e.underlying.Size())
e.underlying.Range(func(k, v string) bool {
d[normalizeKeyName(k)] = v
return true
})
return d
}
// Get returns a key from the environment
func (e *Environment) Get(key string) (string, bool) {
v, ok := e.underlying.Load(normalizeKeyName(key))
return v, ok
}
// Get a boolean value from environment, with a default for empty. Supports true|false, on|off, 1|0
func (e *Environment) GetBool(key string, defaultValue bool) bool {
v, _ := e.Get(key)
switch strings.ToLower(v) {
case "on", "1", "enabled", "true":
return true
case "off", "0", "disabled", "false":
return false
default:
return defaultValue
}
}
// GetInt gets an int value from environment, with a default for unset, empty,
// or invalid values.
func (e *Environment) GetInt(key string, defaultValue int) int {
v, has := e.Get(key)
if !has || v == "" {
return defaultValue
}
x, err := strconv.Atoi(v)
if err != nil {
return defaultValue
}
return x
}
// GetString gets a string value from environment, with a default for unset or
// empty values.
func (e *Environment) GetString(key, defaultValue string) string {
v, has := e.Get(key)
if !has || v == "" {
return defaultValue
}
return v
}
// Exists returns true/false depending on whether or not the key exists in the env
func (e *Environment) Exists(key string) bool {
_, ok := e.underlying.Load(normalizeKeyName(key))
return ok
}
// Set sets a key in the environment
func (e *Environment) Set(key, value string) {
e.underlying.Store(normalizeKeyName(key), value)
}
// Remove a key from the Environment and return its value
func (e *Environment) Remove(key string) string {
value, ok := e.Get(key)
if ok {
e.underlying.Delete(normalizeKeyName(key))
}
return value
}
// Length returns the length of the environment
func (e *Environment) Length() int {
return e.underlying.Size()
}
// Diff returns a new environment with the keys and values from this
// environment which are different in the other one.
func (e *Environment) Diff(other *Environment) Diff {
diff := Diff{
Added: make(map[string]string),
Changed: make(map[string]DiffPair),
Removed: make(map[string]struct{}, 0),
}
if other == nil {
e.underlying.Range(func(k, _ string) bool {
diff.Removed[k] = struct{}{}
return true
})
return diff
}
e.underlying.Range(func(k, v string) bool {
other, ok := other.Get(k)
if !ok {
// This environment has added this key to other
diff.Added[k] = v
return true
}
if other != v {
diff.Changed[k] = DiffPair{
Old: other,
New: v,
}
}
return true
})
other.underlying.Range(func(k, _ string) bool {
if _, ok := e.Get(k); !ok {
diff.Removed[k] = struct{}{}
}
return true
})
return diff
}
// Merge merges another env into this one and returns the result
func (e *Environment) Merge(other *Environment) {
if other == nil {
return
}
other.underlying.Range(func(k, v string) bool {
e.Set(k, v)
return true
})
}
func (e *Environment) Apply(diff Diff) {
for k, v := range diff.Added {
e.Set(k, v)
}
for k, v := range diff.Changed {
e.Set(k, v.New)
}
for k := range diff.Removed {
e.Remove(k)
}
}
// Copy returns a copy of the env
func (e *Environment) Copy() *Environment {
if e == nil {
return New()
}
c := New()
e.underlying.Range(func(k, v string) bool {
c.Set(k, v)
return true
})
return c
}
// ToSlice returns a sorted slice representation of the environment
func (e *Environment) ToSlice() []string {
s := []string{}
e.underlying.Range(func(k, v string) bool {
s = append(s, k+"="+v)
return true
})
// Ensure they are in a consistent order (helpful for tests)
sort.Strings(s)
return s
}
func (e *Environment) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Dump())
}
func (e *Environment) UnmarshalJSON(data []byte) error {
var raw map[string]string
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
e.underlying = xsync.NewMapOfPresized[string](len(raw))
for k, v := range raw {
e.Set(k, v)
}
return nil
}
// Environment variables on Windows are case-insensitive. When you run `SET`
// within a Windows command prompt, you'll see variables like this:
//
// ...
// Path=C:\Program Files (x86)\Parallels\Parallels Tools\Applications;...
// PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
// SystemDrive=C:
// SystemRoot=C:\Windows
// ...
//
// There's a mix of both CamelCase and UPPERCASE, but the can all be accessed
// regardless of the case you use. So PATH is the same as Path, PAth, pATH,
// and so on.
//
// os.Environ() in Golang returns key/values in the original casing, so it
// returns a slice like this:
//
// { "Path=...", "PROCESSOR_IDENTIFIER=...", "SystemRoot=..." }
//
// Users of env.Environment shouldn't need to care about this.
// env.Get("PATH") should "just work" on Windows. This means on Windows
// machines, we'll normalise all the keys that go in/out of this API.
//
// Unix systems _are_ case sensitive when it comes to ENV, so we'll just leave
// that alone.
func normalizeKeyName(key string) string {
if runtime.GOOS == "windows" {
return strings.ToUpper(key)
} else {
return key
}
}
type Diff struct {
Added map[string]string
Changed map[string]DiffPair
Removed map[string]struct{}
}
type DiffPair struct {
Old string
New string
}
func (diff *Diff) Remove(key string) {
delete(diff.Added, key)
delete(diff.Changed, key)
delete(diff.Removed, key)
}
func (diff *Diff) Empty() bool {
return len(diff.Added) == 0 && len(diff.Changed) == 0 && len(diff.Removed) == 0
}
// ProtectedEnv contains environment variables that can only be set by agent configuration.
// These variables cannot be overwritten by job-level environment variables or secrets.
var ProtectedEnv = map[string]struct{}{
"BUILDKITE_AGENT_ACCESS_TOKEN": {},
"BUILDKITE_AGENT_DEBUG": {},
"BUILDKITE_AGENT_ENDPOINT": {},
"BUILDKITE_AGENT_PID": {},
"BUILDKITE_BIN_PATH": {},
"BUILDKITE_BUILD_PATH": {},
"BUILDKITE_COMMAND_EVAL": {},
"BUILDKITE_CONFIG_PATH": {},
"BUILDKITE_CONTAINER_COUNT": {},
"BUILDKITE_GIT_CLEAN_FLAGS": {},
"BUILDKITE_GIT_CLONE_FLAGS": {},
"BUILDKITE_GIT_CLONE_MIRROR_FLAGS": {},
"BUILDKITE_GIT_FETCH_FLAGS": {},
"BUILDKITE_GIT_MIRRORS_LOCK_TIMEOUT": {},
"BUILDKITE_GIT_MIRRORS_PATH": {},
"BUILDKITE_GIT_MIRRORS_SKIP_UPDATE": {},
"BUILDKITE_GIT_SUBMODULES": {},
"BUILDKITE_HOOKS_PATH": {},
"BUILDKITE_KUBERNETES_EXEC": {},
"BUILDKITE_LOCAL_HOOKS_ENABLED": {},
"BUILDKITE_PLUGINS_ENABLED": {},
"BUILDKITE_PLUGINS_PATH": {},
"BUILDKITE_SHELL": {},
"BUILDKITE_HOOKS_SHELL": {},
"BUILDKITE_SSH_KEYSCAN": {},
}
// IsProtected returns true if the given environment variable key is protected.
func IsProtected(key string) bool {
_, exists := ProtectedEnv[key]
return exists
}