-
Notifications
You must be signed in to change notification settings - Fork 3
/
environments.go
58 lines (46 loc) · 1.04 KB
/
environments.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
package cdep
import (
"sort"
"github.com/cuvva/cuvva-public-go/lib/cher"
)
// ListEnvironments returns a set of allowed environments
func ListEnvironments(system string) []string {
allowed := map[string]struct{}{}
switch system {
case "*", "all", "":
for _, envs := range Systems {
for env := range envs {
allowed[env] = struct{}{}
}
}
default:
for env := range Systems[system] {
allowed[env] = struct{}{}
}
}
deduped := []string{}
for key := range allowed {
deduped = append(deduped, key)
}
sort.Strings(deduped)
return deduped
}
// ValidateSystemEnvironment against the allowed options
func ValidateSystemEnvironment(sys, env string) error {
if _, ok := Systems[sys]; !ok {
return cher.New("unknown_system", cher.M{
"sys": sys,
"env": env,
"allowed": ListEnvironments("*"),
})
}
envs := Systems[sys]
if _, ok := envs[env]; !ok && env != "_system" {
return cher.New("unknown_environment", cher.M{
"sys": sys,
"env": env,
"allowed": ListEnvironments(sys),
})
}
return nil
}