forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
67 lines (51 loc) · 1.13 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
package manifestparser
import (
"errors"
"io/ioutil"
yaml "gopkg.in/yaml.v2"
)
type Application struct {
Name string `yaml:"name"`
}
type Parser struct {
PathToManifest string
Applications []Application
rawManifest []byte
}
func NewParser() *Parser {
return new(Parser)
}
func (parser *Parser) Parse(manifestPath string) error {
bytes, err := ioutil.ReadFile(manifestPath)
if err != nil {
return err
}
parser.rawManifest = bytes
var raw struct {
Applications []Application `yaml:"applications"`
}
err = yaml.Unmarshal(bytes, &raw)
if err != nil {
return err
}
parser.Applications = raw.Applications
if len(parser.Applications) == 0 {
return errors.New("must have at least one application")
}
for _, application := range parser.Applications {
if application.Name == "" {
return errors.New("Found an application with no name specified")
}
}
return nil
}
func (parser Parser) AppNames() []string {
var names []string
for _, app := range parser.Applications {
names = append(names, app.Name)
}
return names
}
func (parser Parser) RawManifest(_ string) ([]byte, error) {
return parser.rawManifest, nil
}