forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
74 lines (58 loc) · 1.89 KB
/
parser.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
package manifestparser
import (
"errors"
"io/ioutil"
"github.com/cloudfoundry/bosh-cli/director/template"
"gopkg.in/yaml.v2"
)
type ManifestParser struct{}
// InterpolateAndParse reads the manifest at the provided paths, interpolates
// variables if a vars file is provided, and sets the current manifest to the
// resulting manifest.
// For manifests with only 1 application, appName will override the name of the
// single app defined.
// For manifests with multiple applications, appName will filter the
// applications and leave only a single application in the resulting parsed
// manifest structure.
func (m ManifestParser) InterpolateAndParse(pathToManifest string, pathsToVarsFiles []string, vars []template.VarKV) (Manifest, error) {
rawManifest, err := ioutil.ReadFile(pathToManifest)
if err != nil {
return Manifest{}, err
}
tpl := template.NewTemplate(rawManifest)
fileVars := template.StaticVariables{}
for _, path := range pathsToVarsFiles {
rawVarsFile, ioerr := ioutil.ReadFile(path)
if ioerr != nil {
return Manifest{}, ioerr
}
var sv template.StaticVariables
err = yaml.Unmarshal(rawVarsFile, &sv)
if err != nil {
return Manifest{}, 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 Manifest{}, InterpolationError{Err: err}
}
var parsedManifest Manifest
err = yaml.Unmarshal(rawManifest, &parsedManifest)
if err != nil {
return Manifest{}, err
}
if len(parsedManifest.Applications) == 0 {
return Manifest{}, errors.New("must have at least one application")
}
parsedManifest.PathToManifest = pathToManifest
return parsedManifest, nil
}
func (m ManifestParser) MarshalManifest(manifest Manifest) ([]byte, error) {
return yaml.Marshal(manifest)
}