-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
69 lines (60 loc) · 1.47 KB
/
env.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
package app
import (
"sort"
kapi "k8s.io/kubernetes/pkg/api"
)
// Environment holds environment variables for new-app
type Environment map[string]string
// NewEnvironment returns a new set of environment variables based on all
// the provided environment variables
func NewEnvironment(envs ...map[string]string) Environment {
if len(envs) == 1 {
return envs[0]
}
out := make(Environment)
out.Add(envs...)
return out
}
// Add adds the environment variables to the current environment
func (e Environment) Add(envs ...map[string]string) {
for _, env := range envs {
for k, v := range env {
e[k] = v
}
}
}
// List sorts and returns all the environment variables
func (e Environment) List() []kapi.EnvVar {
env := []kapi.EnvVar{}
for k, v := range e {
env = append(env, kapi.EnvVar{
Name: k,
Value: v,
})
}
sort.Sort(sortedEnvVar(env))
return env
}
type sortedEnvVar []kapi.EnvVar
func (m sortedEnvVar) Len() int { return len(m) }
func (m sortedEnvVar) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m sortedEnvVar) Less(i, j int) bool { return m[i].Name < m[j].Name }
// JoinEnvironment joins two different sets of environment variables
// into one, leaving out all the duplicates
func JoinEnvironment(a, b []kapi.EnvVar) (out []kapi.EnvVar) {
out = a
for i := range b {
exists := false
for j := range a {
if a[j].Name == b[i].Name {
exists = true
break
}
}
if exists {
continue
}
out = append(out, b[i])
}
return out
}