This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
/
parse.go
173 lines (147 loc) · 4.18 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package native
import (
"fmt"
"io"
"io/ioutil"
"os"
"github.com/go-vela/compiler/template/native"
"github.com/go-vela/compiler/template/starlark"
"github.com/go-vela/types/constants"
types "github.com/go-vela/types/yaml"
"github.com/buildkite/yaml"
)
// ParseRaw converts an object to a string.
func (c *client) ParseRaw(v interface{}) (string, error) {
switch v := v.(type) {
case []byte:
return string(v), nil
case *os.File:
return ParseFileRaw(v)
case io.Reader:
return ParseReaderRaw(v)
case string:
// check if string is path to file
_, err := os.Stat(v)
if err == nil {
// parse string as path to yaml configuration
return ParsePathRaw(v)
}
// parse string as yaml configuration
return v, nil
default:
return "", fmt.Errorf("unable to parse yaml: unrecognized type %T", v)
}
}
// Parse converts an object to a yaml configuration.
func (c *client) Parse(v interface{}) (*types.Build, error) {
var p *types.Build
switch c.repo.GetPipelineType() {
case constants.PipelineTypeGo:
// expand the base configuration
parsedRaw, err := c.ParseRaw(v)
if err != nil {
return nil, err
}
p, err = native.RenderBuild(parsedRaw, c.EnvironmentBuild())
if err != nil {
return nil, err
}
case constants.PipelineTypeStarlark:
// expand the base configuration
parsedRaw, err := c.ParseRaw(v)
if err != nil {
return nil, err
}
p, err = starlark.RenderBuild(parsedRaw, c.EnvironmentBuild())
if err != nil {
return nil, err
}
case constants.PipelineTypeYAML, "":
switch v := v.(type) {
case []byte:
return ParseBytes(v)
case *os.File:
return ParseFile(v)
case io.Reader:
return ParseReader(v)
case string:
// check if string is path to file
_, err := os.Stat(v)
if err == nil {
// parse string as path to yaml configuration
return ParsePath(v)
}
// parse string as yaml configuration
return ParseString(v)
default:
return nil, fmt.Errorf("unable to parse yaml: unrecognized type %T", v)
}
default:
// nolint:lll // detailed error message
return nil, fmt.Errorf("unable to parse config: unrecognized pipeline_type of %s", c.repo.GetPipelineType())
}
return p, nil
}
// ParseBytes converts a byte slice to a yaml configuration.
func ParseBytes(b []byte) (*types.Build, error) {
config := new(types.Build)
// unmarshal the bytes into the yaml configuration
err := yaml.Unmarshal(b, config)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal yaml: %v", err)
}
return config, nil
}
// ParseFile converts an os.File into a yaml configuration.
func ParseFile(f *os.File) (*types.Build, error) {
return ParseReader(f)
}
// ParseFileRaw converts an os.File into a string.
func ParseFileRaw(f *os.File) (string, error) {
return ParseReaderRaw(f)
}
// ParsePath converts a file path into a yaml configuration.
func ParsePath(p string) (*types.Build, error) {
// open the file for reading
f, err := os.Open(p)
if err != nil {
return nil, fmt.Errorf("unable to open yaml file %s: %v", p, err)
}
defer f.Close()
return ParseReader(f)
}
// ParsePathRaw converts a file path into a yaml configuration.
func ParsePathRaw(p string) (string, error) {
// open the file for reading
f, err := os.Open(p)
if err != nil {
return "", fmt.Errorf("unable to open yaml file %s: %v", p, err)
}
defer f.Close()
return ParseReaderRaw(f)
}
// ParseReader converts an io.Reader into a yaml configuration.
func ParseReader(r io.Reader) (*types.Build, error) {
// read all the bytes from the reader
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("unable to read bytes for yaml: %v", err)
}
return ParseBytes(b)
}
// ParseReaderRaw converts an io.Reader into a yaml configuration.
func ParseReaderRaw(r io.Reader) (string, error) {
// read all the bytes from the reader
b, err := ioutil.ReadAll(r)
if err != nil {
return "", fmt.Errorf("unable to read bytes for yaml: %v", err)
}
return string(b), nil
}
// ParseString converts a string into a yaml configuration.
func ParseString(s string) (*types.Build, error) {
return ParseBytes([]byte(s))
}