forked from devspace-cloud/penv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment_actions.go
112 lines (98 loc) · 2.88 KB
/
environment_actions.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
package penv
import "fmt"
// DAO defines the interface for loading and saving a set of environment
// variables
type DAO interface {
Load() (*Environment, error)
Save(*Environment) error
}
type (
// Environment is a collection of appenders, setters and unsetters
Environment struct {
Appenders []NameValue
Setters []NameValue
Unsetters []NameValue
}
// NameValue is a name value pair
NameValue struct {
Name string
Value string
}
// EnvironmentActions Performs actions on the env DAO's
EnvironmentActions struct {
dao DAO
}
)
func filter(arr []NameValue, cond func(NameValue) bool) []NameValue {
nvs := make([]NameValue, 0, len(arr))
for _, nv := range arr {
if cond(nv) {
nvs = append(nvs, nv)
}
}
return nvs
}
// AppendEnv permanently appends an environment variable
func (environmentActions *EnvironmentActions) AppendEnv(name, value string) error {
env, err := environmentActions.dao.Load()
if err != nil {
return fmt.Errorf("failed to load environment: %v", err)
}
env.Setters = filter(env.Setters, func(nv NameValue) bool {
return nv.Name != name || nv.Value != value
})
env.Appenders = filter(env.Appenders, func(nv NameValue) bool {
return nv.Name != name || nv.Value != value
})
env.Appenders = append(env.Appenders, NameValue{name, value})
// if it's being unset, remove it from the list
env.Unsetters = filter(env.Unsetters, func(nv NameValue) bool {
return nv.Name != name
})
err = environmentActions.dao.Save(env)
if err != nil {
return fmt.Errorf("failed to save environment: %v", err)
}
return nil
}
// SetEnv permanently sets an environment variable
func (environmentActions *EnvironmentActions) SetEnv(name, value string) error {
env, err := environmentActions.dao.Load()
if err != nil {
return fmt.Errorf("failed to load environment: %v", err)
}
env.Setters = filter(env.Setters, func(nv NameValue) bool {
return nv.Name != name
})
env.Setters = append(env.Setters, NameValue{name, value})
env.Unsetters = filter(env.Unsetters, func(nv NameValue) bool {
return nv.Name != name
})
err = environmentActions.dao.Save(env)
if err != nil {
return fmt.Errorf("failed to save environment: %v", err)
}
return nil
}
// UnsetEnv permanently unsets an environment variable
func (environmentActions *EnvironmentActions) UnsetEnv(name string) error {
env, err := environmentActions.dao.Load()
if err != nil {
return fmt.Errorf("failed to load environment: %v", err)
}
env.Setters = filter(env.Setters, func(nv NameValue) bool {
return nv.Name != name
})
env.Appenders = filter(env.Appenders, func(nv NameValue) bool {
return nv.Name != name
})
env.Unsetters = filter(env.Unsetters, func(nv NameValue) bool {
return nv.Name != name
})
env.Unsetters = append(env.Unsetters, NameValue{name, ""})
err = environmentActions.dao.Save(env)
if err != nil {
return fmt.Errorf("failed to save environment: %v", err)
}
return nil
}