-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathparse.go
279 lines (236 loc) · 6.35 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package dog
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"github.com/ghodss/yaml"
)
// DefaultRunner defines the runner to use in case the task does not specify it.
var DefaultRunner = "sh"
// ErrMalformedStringArray means that a task have a value of
// pre, post or env that can't be parsed as an array of strings.
var ErrMalformedStringArray = errors.New("Malformed strings array")
// ErrNoDogfile means that the application is unable to find
// a Dogfile in the specified directory.
var ErrNoDogfile = errors.New("No dogfile found")
// Dogtasks is a collection of tasks with optional metadata from the runtime.
type Dogtasks struct {
// Tasks is used to map task objects by their name.
Tasks map[string]*Task
// Path is an optional field that stores the directory
// where the Dogfile is found.
Path string
// Files is an optional field that stores the full path
// of each Dogfile used to define the Dogtasks object.
Files []string
}
// TaskYAML represents a task written in the Dogfile format.
type taskYAML struct {
Name string `json:"task"`
Description string `json:"description,omitempty"`
Code string `json:"code"`
Run string `json:"run"` // backwards compatibility for 'code'
Runner string `json:"runner,omitempty"`
Exec string `json:"exec,omitempty"` // backwards compatibility for 'runner'
Pre interface{} `json:"pre,omitempty"`
Post interface{} `json:"post,omitempty"`
Env interface{} `json:"env,omitempty"`
Workdir string `json:"workdir,omitempty"`
Register string `json:"register,omitempty"`
}
// Parse accepts a slice of bytes and parses it following the Dogfile Spec.
func Parse(p []byte) (dtasks Dogtasks, err error) {
var tasks []*taskYAML
err = yaml.Unmarshal(p, &tasks)
if err != nil {
return
}
for _, parsedTask := range tasks {
if _, ok := dtasks.Tasks[parsedTask.Name]; ok {
err = fmt.Errorf("Duplicated task name %s", parsedTask.Name)
return
} else if !validTaskName(parsedTask.Name) {
err = fmt.Errorf("Invalid name for task %s", parsedTask.Name)
return
} else {
task := &Task{
Name: parsedTask.Name,
Description: parsedTask.Description,
Code: parsedTask.Code,
Runner: parsedTask.Runner,
Workdir: parsedTask.Workdir,
Register: parsedTask.Register,
}
// convert pre-tasks, post-tasks and environment variables
// into []string
if task.Pre, err = parseStringSlice(parsedTask.Pre); err != nil {
return
}
if task.Post, err = parseStringSlice(parsedTask.Post); err != nil {
return
}
if task.Env, err = parseStringSlice(parsedTask.Env); err != nil {
return
}
// set default runner if not specified
if task.Runner == "" {
task.Runner = DefaultRunner
}
if dtasks.Tasks == nil {
dtasks.Tasks = make(map[string]*Task)
}
dtasks.Tasks[task.Name] = task
}
}
// validate resulting dogtasks object
err = dtasks.Validate()
return
}
// parseStringSlice takes an interface from a pre, post or env field
// and returns a slice of strings representing the found values.
func parseStringSlice(str interface{}) ([]string, error) {
switch h := str.(type) {
case string:
return []string{h}, nil
case []interface{}:
s := make([]string, len(h))
for i, hook := range h {
sHook, ok := hook.(string)
if !ok {
return nil, ErrMalformedStringArray
}
s[i] = sHook
}
return s, nil
case nil:
return []string{}, nil
default:
return nil, ErrMalformedStringArray
}
}
// ParseFromDisk finds a Dogfile in disk and parses it.
func ParseFromDisk(dir string) (dtasks Dogtasks, err error) {
if dir == "" {
dir = "."
}
dir, err = filepath.Abs(dir)
if err != nil {
return
}
dtasks.Files, err = FindDogfiles(dir)
if err != nil {
return
}
if len(dtasks.Files) == 0 {
err = ErrNoDogfile
return
}
dtasks.Path, err = filepath.Abs(filepath.Dir(dtasks.Files[0]))
if err != nil {
return
}
// iterate over every found file
for _, file := range dtasks.Files {
var fileData []byte
var d Dogtasks
fileData, err = ioutil.ReadFile(file)
if err != nil {
return
}
// parse file
d, err = Parse(fileData)
if err != nil {
return
}
// add parsed tasks to main dogfile
for _, t := range d.Tasks {
if dtasks.Tasks == nil {
dtasks.Tasks = make(map[string]*Task)
}
if t.Workdir == "" {
t.Workdir = dtasks.Path
} else {
t.Workdir, err = filepath.Abs(t.Workdir)
if err != nil {
return
}
}
dtasks.Tasks[t.Name] = t
}
}
// validate resulting dogfile
err = dtasks.Validate()
return
}
// Validate checks that all tasks in a Dogfile are valid.
//
// It checks if any task has a non standard name and also if the
// resulting task chain of each of them have an undesired cycle.
func (dtasks *Dogtasks) Validate() error {
for _, t := range dtasks.Tasks {
if !validTaskName(t.Name) {
return fmt.Errorf("Invalid name for task %s", t.Name)
}
if _, err := NewTaskChain(*dtasks, t.Name); err != nil {
return err
}
}
return nil
}
// FindDogfiles finds Dogfiles in disk for a given path.
//
// It traverses directories until it finds one containing Dogfiles.
// If such a directory is found, the function returns the full path
// for each valid Dogfile in that directory.
func FindDogfiles(p string) ([]string, error) {
var dogfilePaths []string
currentPath, err := filepath.Abs(p)
if err != nil {
return nil, err
}
for {
var files []os.FileInfo
files, err = ioutil.ReadDir(currentPath)
if err != nil {
return nil, err
}
for _, file := range files {
if validDogfileName(file.Name()) {
dogfilePath := path.Join(currentPath, file.Name())
dogfilePaths = append(dogfilePaths, dogfilePath)
}
}
if len(dogfilePaths) > 0 {
return dogfilePaths, nil
}
nextPath := path.Dir(currentPath)
if nextPath == currentPath {
return dogfilePaths, nil
}
currentPath = nextPath
}
}
// validDogfileName checks if a Dogfile name is valid as defined
// by the Dogfile Spec.
func validDogfileName(name string) bool {
var match bool
match, err := regexp.MatchString("^(dog|🐕).*\\.(yml|yaml)$", name)
if err != nil {
return false
}
return match
}
// validTaskName checks if a task name is valid as defined
// by the Dogfile Spec.
func validTaskName(name string) bool {
var match bool
match, err := regexp.MatchString("^[a-z0-9]+(-[a-z0-9]+)*$", name)
if err != nil {
return false
}
return match
}