-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.go
84 lines (73 loc) · 1.97 KB
/
pipeline.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
package factory
import (
"log"
"github.com/hashicorp/hcl/v2"
)
// pipelineBlockSchema is the schema for a top-level "pipeline" block in
// a configuration file.
var pipelineBlockSchema = &hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{Name: "stages", Required: true},
},
Blocks: []hcl.BlockHeaderSchema{
{
Type: "filter",
},
},
}
type StageDefinition struct {
Name string
DependsOn []string
Namespaces []string
}
type Pipeline struct {
Name string
Filter *Filter
Stages []*StageDefinition
}
func NewPipeline() *Pipeline {
return &Pipeline{
Stages: make([]*StageDefinition, 0),
}
}
func decodePipelineBlock(block *hcl.Block, file *File) (*Pipeline, hcl.Diagnostics) {
content, diags := block.Body.Content(pipelineBlockSchema)
pipeline := NewPipeline()
pipeline.Name = block.Labels[0]
for _, innerBlock := range content.Blocks {
switch innerBlock.Type {
case "filter":
log.Printf("[DEBUG] Filter block found, decoding in progress")
filterCfg, filterDiags := decodeFilterBlock(innerBlock)
diags = append(diags, filterDiags...)
pipeline.Filter = filterCfg
default:
// Should never happen beacause the above cases should be exhaustive
// for all block type names in our schema.
continue
}
}
// Add the stages
stages := content.Attributes["stages"]
stagesVal, d := stages.Expr.Value(file.GetEvalContext(nil))
diags = append(diags, d...)
stageDefs := make([]*StageDefinition, 0)
for _, el := range stagesVal.AsValueSlice() {
elMap := el.AsValueMap()
sd := &StageDefinition{}
sd.Name = elMap["name"].AsString()
if dependsOn, ok := elMap["depends_on"]; ok {
for _, dep := range dependsOn.AsValueSlice() {
sd.DependsOn = append(sd.DependsOn, dep.AsString())
}
}
if namespaces, ok := elMap["namespaces"]; ok {
for _, ns := range namespaces.AsValueSlice() {
sd.Namespaces = append(sd.Namespaces, ns.AsString())
}
}
stageDefs = append(stageDefs, sd)
}
pipeline.Stages = stageDefs
return pipeline, diags
}