-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmanifest.go
64 lines (55 loc) · 1.4 KB
/
manifest.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 out
import (
"gopkg.in/yaml.v2"
"io/ioutil"
)
type Manifest struct {
data map[interface{}]interface{}
}
func NewManifest(manifestPath string) (Manifest, error) {
yamlData, err := ioutil.ReadFile(manifestPath)
if err != nil {
return Manifest{}, err
}
var manifest Manifest
err = yaml.Unmarshal(yamlData, &manifest.data)
if err != nil {
return Manifest{}, err
}
return manifest, nil
}
func (manifest *Manifest) EnvironmentVariables() []map[interface{}]interface{} {
apps, hasApps := manifest.data["applications"].([]interface{})
if !hasApps {
return []map[interface{}]interface{}{}
}
appEnvVars := make([]map[interface{}]interface{}, len(apps))
for appIdx := range apps {
app, isApp := apps[appIdx].(map[interface{}]interface{})
if !isApp {
continue
}
envVars, hasEnvVars := app["env"].(map[interface{}]interface{})
if !hasEnvVars {
envVars = make(map[interface{}]interface{})
app["env"] = envVars
}
appEnvVars[appIdx] = envVars
}
return appEnvVars
}
func (manifest *Manifest) AddEnvironmentVariable(name, value string) {
appEnvVars := manifest.EnvironmentVariables()
for appIdx := range appEnvVars {
if appEnvVars[appIdx] != nil {
appEnvVars[appIdx][name] = value
}
}
}
func (manifest *Manifest) Save(manifestPath string) error {
data, err := yaml.Marshal(manifest.data)
if err != nil {
return err
}
return ioutil.WriteFile(manifestPath, data, 0644)
}