forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
114 lines (92 loc) · 2.89 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
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
113
114
package manifest
import (
"io/ioutil"
"path/filepath"
"github.com/cloudfoundry/bosh-cli/director/template"
yaml "gopkg.in/yaml.v2"
)
type Manifest struct {
Applications []Application `yaml:"applications"`
}
func (manifest *Manifest) UnmarshalYAML(unmarshal func(interface{}) error) error {
var raw rawManifest
err := unmarshal(&raw)
if err != nil {
return err
}
if raw.containsInheritanceField() {
return InheritanceFieldError{}
}
if globals := raw.containsGlobalFields(); len(globals) > 0 {
return GlobalFieldsError{Fields: globals}
}
manifest.Applications = raw.Applications
return nil
}
// ReadAndInterpolateManifest reads the manifest at the provided paths,
// interpolates variables if a vars file is provided, and returns a fully
// merged set of applications.
func ReadAndInterpolateManifest(pathToManifest string, pathsToVarsFiles []string, vars []template.VarKV) ([]Application, error) {
rawManifest, err := ReadAndInterpolateRawManifest(pathToManifest, pathsToVarsFiles, vars)
if err != nil {
return nil, err
}
var manifest Manifest
err = yaml.Unmarshal(rawManifest, &manifest)
if err != nil {
return nil, err
}
for i, app := range manifest.Applications {
if app.Path != "" && !filepath.IsAbs(app.Path) {
manifest.Applications[i].Path = filepath.Join(filepath.Dir(pathToManifest), app.Path)
}
}
return manifest.Applications, err
}
// ReadAndInterpolateRawManifest reads the manifest at the provided paths,
// interpolates variables if a vars file is provided, and returns the
// Unmarshalled result.
func ReadAndInterpolateRawManifest(pathToManifest string, pathsToVarsFiles []string, vars []template.VarKV) ([]byte, error) {
rawManifest, err := ioutil.ReadFile(pathToManifest)
if err != nil {
return nil, err
}
tpl := template.NewTemplate(rawManifest)
fileVars := template.StaticVariables{}
for _, path := range pathsToVarsFiles {
rawVarsFile, ioerr := ioutil.ReadFile(path)
if ioerr != nil {
return nil, ioerr
}
var sv template.StaticVariables
err = yaml.Unmarshal(rawVarsFile, &sv)
if err != nil {
return nil, InvalidYAMLError{Err: err}
}
for k, v := range sv {
fileVars[k] = v
}
}
for _, kv := range vars {
fileVars[kv.Name] = kv.Value
}
rawManifest, err = tpl.Evaluate(fileVars, nil, template.EvaluateOpts{ExpectAllKeys: true})
if err != nil {
return nil, InterpolationError{Err: err}
}
return rawManifest, nil
}
// WriteApplicationManifest writes the provided application to the given
// filepath. If the filepath does not exist, it will create it.
func WriteApplicationManifest(application Application, filePath string) error {
manifest := Manifest{Applications: []Application{application}}
manifestBytes, err := yaml.Marshal(manifest)
if err != nil {
return ManifestCreationError{Err: err}
}
err = ioutil.WriteFile(filePath, manifestBytes, 0644)
if err != nil {
return ManifestCreationError{Err: err}
}
return nil
}