forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
98 lines (78 loc) · 2.27 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
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 retunrs a fully
// merged set of applications.
func ReadAndInterpolateManifest(pathToManifest string, pathToVarsFile string) ([]Application, error) {
rawManifest, err := ioutil.ReadFile(pathToManifest)
if err != nil {
return nil, err
}
tpl := template.NewTemplate(rawManifest)
if pathToVarsFile != "" {
var (
rawVarsFile []byte
staticVars template.StaticVariables
)
rawVarsFile, err = ioutil.ReadFile(pathToVarsFile)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(rawVarsFile, &staticVars)
if err != nil {
return nil, InvalidYAMLError{Err: err}
}
rawManifest, err = tpl.Evaluate(staticVars, nil, template.EvaluateOpts{})
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
}
// 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
}