forked from redhat-developer/kam
/
parser.go
49 lines (44 loc) · 1.1 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
package config
import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"github.com/spf13/afero"
"sigs.k8s.io/yaml"
)
// Parse decodes YAML describing an environment manifest.
func Parse(in io.Reader) (*Manifest, error) {
m := &Manifest{}
buf, err := ioutil.ReadAll(in)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(buf, m)
if err != nil {
return nil, err
}
return m, nil
}
// ParseFile is a wrapper around Parse that accepts a filename, it opens and
// parses the file, and closes it.
func ParseFile(fs afero.Fs, filename string) (*Manifest, error) {
f, err := fs.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return Parse(f)
}
// ParsePipelinesFolder will accept the pipelines folder path
// and appends pipelines file name before parsing it
func ParsePipelinesFolder(fs afero.Fs, folderPath string) (*Manifest, error) {
info, err := fs.Stat(folderPath)
if err != nil {
return nil, err
}
if !info.IsDir() {
return nil, fmt.Errorf("The path %q is a file path (required directory path)", folderPath)
}
return ParseFile(fs, filepath.Join(folderPath, PipelinesFile))
}