forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
64 lines (56 loc) · 1.36 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
package app
import (
"sort"
kapi "github.com/GoogleCloudPlatform/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)
for _, env := range envs {
for k, v := range env {
out[k] = v
}
}
return out
}
// 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
}