-
Notifications
You must be signed in to change notification settings - Fork 18
/
pipestep.go
145 lines (117 loc) · 4.29 KB
/
pipestep.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
package task
import (
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-cli/pkg/parser/nameable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/node"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parseable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parserkit"
"github.com/cirruslabs/cirrus-cli/pkg/parser/schema"
"github.com/cirruslabs/cirrus-cli/pkg/parser/task/command"
jsschema "github.com/lestrrat-go/jsschema"
"strings"
)
type PipeStep struct {
protoCommands []*api.Command
image string
parseable.DefaultParser
}
func NewPipeStep(
mergedEnv map[string]string,
parserKit *parserkit.ParserKit,
previousCommands []*api.Command,
) *PipeStep {
step := &PipeStep{}
imageSchema := schema.String("Docker Image to use.")
step.RequiredField(nameable.NewSimpleNameable("image"), imageSchema, func(node *node.Node) error {
image, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
step.image = image
return nil
})
scriptNameable := nameable.NewRegexNameable("^(.*)script$")
step.OptionalField(scriptNameable, schema.Script(""), func(node *node.Node) error {
command, err := handleScript(node, scriptNameable)
if err != nil {
return err
}
step.protoCommands = append(step.protoCommands, command)
return nil
})
cacheNameable := nameable.NewRegexNameable("^(.*)cache$")
cacheSchema := command.NewCacheCommand(nil, nil).Schema()
step.OptionalField(cacheNameable, cacheSchema, func(node *node.Node) error {
cache := command.NewCacheCommand(mergedEnv, parserKit)
if err := cache.Parse(node, parserKit); err != nil {
return err
}
step.protoCommands = append(step.protoCommands, cache.Proto())
return nil
})
uploadCachesNameable := nameable.NewSimpleNameable("upload_caches")
step.OptionalRepeatableField(uploadCachesNameable, command.UploadCachesSchema(), func(node *node.Node) error {
commandsToAppend, err := command.UploadCachesHelper(mergedEnv, append(previousCommands, step.protoCommands...), node)
if err != nil {
return err
}
step.protoCommands = append(step.protoCommands, commandsToAppend...)
return nil
})
artifactsNameable := nameable.NewRegexNameable("^(.*)artifacts$")
artifactsSchema := command.NewArtifactsCommand(nil).Schema()
step.OptionalField(artifactsNameable, artifactsSchema, func(node *node.Node) error {
artifacts := command.NewArtifactsCommand(mergedEnv)
if err := artifacts.Parse(node, parserKit); err != nil {
return err
}
step.protoCommands = append(step.protoCommands, artifacts.Proto())
return nil
})
fileNameable := nameable.NewRegexNameable("^(.*)file$")
fileSchema := command.NewFileCommand(nil).Schema()
step.OptionalField(fileNameable, fileSchema, func(node *node.Node) error {
file := command.NewFileCommand(mergedEnv)
if err := file.Parse(node, parserKit); err != nil {
return err
}
step.protoCommands = append(step.protoCommands, file.Proto())
return nil
})
for id, name := range api.Command_CommandExecutionBehavior_name {
idCopy := id
behaviorName := nameable.NewSimpleNameable(strings.ToLower(name))
behaviorSchema := NewBehavior(nil, nil, nil).Schema()
behaviorSchema.Description = name + " commands."
step.OptionalRepeatableField(behaviorName, behaviorSchema, func(node *node.Node) error {
behavior := NewBehavior(mergedEnv, parserKit, append(previousCommands, step.protoCommands...))
if err := behavior.Parse(node, parserKit); err != nil {
return err
}
commands := behavior.Proto()
for _, command := range commands {
command.ExecutionBehaviour = api.Command_CommandExecutionBehavior(idCopy)
step.protoCommands = append(step.protoCommands, command)
}
return nil
})
}
return step
}
func (step *PipeStep) Parse(node *node.Node, parserKit *parserkit.ParserKit) error {
if err := step.DefaultParser.Parse(node, parserKit); err != nil {
return err
}
if len(step.protoCommands) == 0 {
return node.ParserError("pipe steps defined without scripts inside them")
}
step.protoCommands[0].Properties = make(map[string]string)
step.protoCommands[0].Properties["image"] = step.image
return nil
}
func (step *PipeStep) Schema() *jsschema.Schema {
modifiedSchema := step.DefaultParser.Schema()
modifiedSchema.Type = jsschema.PrimitiveTypes{jsschema.ObjectType}
modifiedSchema.Description = "Pipe step"
return modifiedSchema
}