forked from buildkite/go-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
47 lines (40 loc) · 1.38 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
package pipeline
import (
"errors"
"io"
"strings"
"github.com/buildkite/go-pipeline/ordered"
"gopkg.in/yaml.v3"
)
// Options are functional options for creating a new Env.
type Options func(*Pipeline)
// Parse parses a pipeline. It does not apply interpolation.
// Warnings are passed through the err return:
//
// p, err := Parse(src)
// if w := warning.As(err); w != nil {
// // Here are some warnings that should be shown
// log.Printf("*Warning* - pipeline is not fully parsed:\n%v", w)
// } else if err != nil {
// // Parse could not understand src at all
// return err
// }
// // Use p
func Parse(src io.Reader) (*Pipeline, error) {
// First get yaml.v3 to give us a raw document (*yaml.Node).
n := new(yaml.Node)
if err := yaml.NewDecoder(src).Decode(n); err != nil {
return nil, formatYAMLError(err)
}
// Instead of unmarshalling into structs, which is easy-ish to use but
// doesn't work with some non YAML 1.2 features (merges), decode the
// *yaml.Node into *ordered.Map, []any, or any (recursively).
// This resolves aliases and merges and gives a more convenient form to work
// with when handling different structural representations of the same
// configuration. Then decode _that_ into a pipeline.
p := new(Pipeline)
return p, ordered.Unmarshal(n, p)
}
func formatYAMLError(err error) error {
return errors.New(strings.TrimPrefix(err.Error(), "yaml: "))
}