-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
153 lines (144 loc) · 4.83 KB
/
file.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
package factory
import (
"fmt"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
)
type File struct {
Pipelines []*Pipeline
Variables *Variables
Stages []*Stage
}
func (f *File) GetEvalContext(scopeID *string) *hcl.EvalContext {
v := f.Variables
// No Scope, get the global context
if scopeID == nil {
if len(v.GlobalVariables) == 0 {
return nil
}
return &hcl.EvalContext{
Variables: map[string]cty.Value{
"var": cty.MapVal(v.GlobalVariables),
},
}
}
// Combine the stage Scope with the global scope, overriding global variables
scope := make(map[string]cty.Value)
// First add the global variables
for k, v := range v.GlobalVariables {
scope[k] = v
}
if stageScope, ok := v.StageVariables[*scopeID]; ok {
// Add the stage scope
for k, v := range stageScope {
scope[k] = v
}
}
// Cannot create MapVal with an empty map, so return nil
if len(scope) == 0 {
return nil
}
return &hcl.EvalContext{
Variables: map[string]cty.Value{
"var": cty.MapVal(scope),
},
}
}
func NewFile() *File {
return &File{
Pipelines: make([]*Pipeline, 0),
Variables: NewVariables(),
Stages: make([]*Stage, 0),
}
}
func (f *File) String() string {
var sb strings.Builder
sb.WriteString("File {\n")
// Filters
sb.WriteString(fmt.Sprintf("%s Pipeline: [\n", indent(1)))
for _, pipeline := range f.Pipelines {
// Filter
filter := pipeline.Filter
sb.WriteString(fmt.Sprintf("%s %s: {\n", indent(2), pipeline.Name))
sb.WriteString(fmt.Sprintf("%s Filter: {\n", indent(3)))
sb.WriteString(fmt.Sprintf("%s Exclude: {\n", indent(4)))
sb.WriteString(fmt.Sprintf("%s Paths: [\n", indent(5)))
for _, path := range filter.Exclude.Paths {
sb.WriteString(fmt.Sprintf("%s %s\n", indent(6), path))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(5)))
sb.WriteString(fmt.Sprintf("%s Branches: [\n", indent(5)))
for _, branch := range filter.Exclude.Branches {
sb.WriteString(fmt.Sprintf("%s %s\n", indent(6), branch))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(5)))
sb.WriteString(fmt.Sprintf("%s }\n", indent(4)))
sb.WriteString(fmt.Sprintf("%s Include {\n", indent(4)))
sb.WriteString(fmt.Sprintf("%s Paths: [\n", indent(5)))
for _, path := range filter.Include.Paths {
sb.WriteString(fmt.Sprintf("%s %s\n", indent(6), path))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(5)))
sb.WriteString(fmt.Sprintf("%s Branches: [\n", indent(5)))
for _, branch := range filter.Include.Branches {
sb.WriteString(fmt.Sprintf("%s %s\n", indent(6), branch))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(5)))
sb.WriteString(fmt.Sprintf("%s }\n", indent(4)))
sb.WriteString(fmt.Sprintf("%s }\n", indent(3)))
// Stages
sb.WriteString(fmt.Sprintf("%s Stage Definitions: [\n", indent(3)))
for _, stage := range pipeline.Stages {
sb.WriteString(fmt.Sprintf("%s {\n", indent(4)))
sb.WriteString(fmt.Sprintf("%s Name: %s\n", indent(5), stage.Name))
sb.WriteString(fmt.Sprintf("%s Depends On: [\n", indent(5)))
for _, dp := range stage.DependsOn {
sb.WriteString(fmt.Sprintf("%s %s\n", indent(6), dp))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(5)))
sb.WriteString(fmt.Sprintf("%s }\n", indent(4)))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(3)))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(2)))
// Variables
sb.WriteString(fmt.Sprintf("%s Global Variables: [\n", indent(2)))
for k, v := range f.Variables.GlobalVariables {
sb.WriteString(fmt.Sprintf("%s {%s: %s}\n", indent(3), k, v.AsString()))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(2)))
sb.WriteString(fmt.Sprintf("%s Stage Variables: [\n", indent(2)))
for k, v := range f.Variables.StageVariables {
sb.WriteString(fmt.Sprintf("%s %s: [\n", indent(3), k))
for variable, value := range v {
sb.WriteString(fmt.Sprintf("%s {%s: %s}\n", indent(4), variable, value.AsString()))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(3)))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(2)))
// Stages
sb.WriteString(fmt.Sprintf("%s Stages: [\n", indent(2)))
for _, stage := range f.Stages {
sb.WriteString(fmt.Sprintf("%s %s: [\n", indent(3), stage.Name))
for _, runBlock := range stage.RunBlocks {
sb.WriteString(fmt.Sprintf("%s %s: [\n", indent(4), runBlock.Name))
for _, command := range runBlock.Commands {
sb.WriteString(fmt.Sprintf("%s {\n", indent(5)))
sb.WriteString(fmt.Sprintf("%s command: %s\n", indent(6), command))
sb.WriteString(fmt.Sprintf("%s }\n", indent(5)))
}
if runBlock.File != "" {
sb.WriteString(fmt.Sprintf("%s { file: %s }\n", indent(5), runBlock.File))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(4)))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(3)))
}
sb.WriteString(fmt.Sprintf("%s ]\n", indent(2)))
sb.WriteString(fmt.Sprintf("%s }\n\n", indent(1)))
return sb.String()
}
func indent(spaces int) string {
return strings.Repeat(" ", spaces)
}