forked from drone/go-convert
/
parse.go
55 lines (48 loc) · 1.04 KB
/
parse.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
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Polyform License
// that can be found in the LICENSE file.
package yaml
import (
"bytes"
"io"
"os"
buildkiteYaml "github.com/buildkite/yaml"
)
// Parse parses the configuration from io.Reader r.
func Parse(r io.Reader) ([]*Pipeline, error) {
res := []*Pipeline{}
dec := buildkiteYaml.NewDecoder(r)
for {
out := new(Pipeline)
err := dec.Decode(out)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
res = append(res, out)
}
return res, nil
}
// ParseBytes parses the configuration from bytes b.
func ParseBytes(b []byte) ([]*Pipeline, error) {
return Parse(
bytes.NewBuffer(b),
)
}
// ParseString parses the configuration from string s.
func ParseString(s string) ([]*Pipeline, error) {
return ParseBytes(
[]byte(s),
)
}
// ParseFile parses the configuration from path p.
func ParseFile(p string) ([]*Pipeline, error) {
f, err := os.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
return Parse(f)
}