-
Notifications
You must be signed in to change notification settings - Fork 9
/
scaffold.go
166 lines (138 loc) · 3.51 KB
/
scaffold.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
package scaffold
import (
"os"
"path/filepath"
"github.com/pluralsh/plural-cli/pkg/executor"
"github.com/pluralsh/plural-cli/pkg/utils/git"
"github.com/pluralsh/plural-cli/pkg/utils/pathing"
"github.com/pluralsh/plural-cli/pkg/wkspace"
"github.com/pluralsh/polly/algorithms"
"github.com/pluralsh/polly/containers"
"github.com/rodaine/hclencoder"
)
type Scaffold struct {
Name string `hcl:",key"`
Path string `hcl:"path"`
Type string `hcl:"type"`
Root string `hcle:"omit"`
Preflight []*executor.Step `hcl:"preflight"`
}
type Metadata struct {
Name string `hcl:"name"`
}
type Build struct {
Metadata *Metadata `hcl:"metadata"`
Scaffolds []*Scaffold `hcl:"scaffold"`
}
const (
TF = "terraform"
HELM = "helm"
CRD = "crd"
)
func Scaffolds(wk *wkspace.Workspace) (*Build, error) {
repoRoot, err := git.Root()
if err != nil {
return &Build{}, err
}
name := wk.Installation.Repository.Name
wkspaceRoot := pathing.SanitizeFilepath(filepath.Join(repoRoot, name))
build, err := Read(wkspaceRoot)
def := Default(wk, name)
if err != nil {
return def, nil
}
return merge(build, def), nil
}
func merge(build *Build, base *Build) *Build {
byName := make(map[string]*Scaffold)
for _, scaffold := range build.Scaffolds {
byName[scaffold.Name] = scaffold
}
for _, scaffold := range base.Scaffolds {
if prev, ok := byName[scaffold.Name]; ok {
mergePreflights(scaffold, prev)
}
byName[scaffold.Name] = scaffold
}
// to handle helm v3 transition
delete(byName, "add-repo")
graph := containers.NewGraph[string]()
for i := 0; i < len(build.Scaffolds)-1; i++ {
graph.AddEdge(build.Scaffolds[i].Name, build.Scaffolds[i+1].Name)
}
for i := 0; i < len(base.Scaffolds)-1; i++ {
graph.AddEdge(base.Scaffolds[i].Name, base.Scaffolds[i+1].Name)
}
sorted, _ := algorithms.TopsortGraph(graph)
build.Scaffolds = algorithms.Map(sorted, func(n string) *Scaffold { return byName[n] })
return build
}
func mergePreflights(new, old *Scaffold) {
byName := make(map[string]*executor.Step)
for _, preflight := range old.Preflight {
byName[preflight.Name] = preflight
}
for _, preflight := range new.Preflight {
if prev, ok := byName[preflight.Name]; ok {
preflight.Sha = prev.Sha
}
}
}
func (b *Build) Flush(root string) error {
io, err := hclencoder.Encode(&b)
if err != nil {
return err
}
path, _ := filepath.Abs(pathing.SanitizeFilepath(filepath.Join(root, b.Metadata.Name, "build.hcl")))
return os.WriteFile(path, io, 0644)
}
func (s *Scaffold) Execute(wk *wkspace.Workspace, force bool) error {
os.Setenv("HELM_REPO_ACCESS_TOKEN", wk.Config.Token)
err := s.executeType(wk)
if err != nil {
return err
}
ignore := []string{}
for _, preflight := range s.Preflight {
if force {
preflight.Sha = ""
}
sha, err := preflight.Execute(s.Root, ignore)
if err != nil {
return err
}
preflight.Sha = sha
}
return nil
}
func (s *Scaffold) executeType(wk *wkspace.Workspace) error {
switch s.Type {
case TF:
return s.handleTerraform(wk)
case HELM:
return s.handleHelm(wk)
case CRD:
return s.buildCrds(wk)
default:
return nil
}
}
func (b *Build) Execute(wk *wkspace.Workspace, force bool) error {
root, err := git.Root()
if err != nil {
return err
}
for _, s := range b.Scaffolds {
path := pathing.SanitizeFilepath(filepath.Join(root, b.Metadata.Name, s.Path))
if err := os.MkdirAll(path, os.ModePerm); err != nil {
b.Flush(root)
return err
}
s.Root = path
if err := s.Execute(wk, force); err != nil {
b.Flush(root)
return err
}
}
return b.Flush(root)
}