/
builder.go
139 lines (114 loc) · 3.05 KB
/
builder.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
package diff
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/hcl"
"github.com/pluralsh/plural-cli/pkg/executor"
"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/pluralsh/plural-cli/pkg/utils/git"
"github.com/pluralsh/plural-cli/pkg/utils/pathing"
"github.com/pluralsh/polly/algorithms"
"github.com/pluralsh/polly/containers"
"github.com/rodaine/hclencoder"
)
type Diff struct {
Metadata Metadata `hcl:"metadata"`
Steps []*executor.Step `hcl:"step"`
}
type Metadata struct {
Path string `hcl:"path"`
Name string `hcl:"name"`
}
func GetDiff(path, name string) (*Diff, error) {
fullpath := pathing.SanitizeFilepath(filepath.Join(path, name+".hcl"))
contents, err := os.ReadFile(fullpath)
diff := Diff{}
if err != nil {
return &diff, nil
}
err = hcl.Decode(&diff, string(contents))
return &diff, err
}
func (e *Diff) Execute() error {
root, err := git.Root()
if err != nil {
return err
}
path := pathing.SanitizeFilepath(filepath.Join(root, "diffs"))
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
}
if err := utils.EmptyDirectory(path); err != nil {
return err
}
ignore, err := e.IgnoreFile(root)
if err != nil {
return err
}
fmt.Printf("deploying %s, hold on to your butts\n", e.Metadata.Path)
for i, step := range e.Steps {
newSha, err := step.Execute(root, ignore)
if err != nil {
if err := e.Flush(root); err != nil {
return err
}
return err
}
e.Steps[i].Sha = newSha
}
return e.Flush(root)
}
func (e *Diff) IgnoreFile(root string) ([]string, error) {
ignorePath := pathing.SanitizeFilepath(filepath.Join(root, e.Metadata.Path, ".pluralignore"))
contents, err := os.ReadFile(ignorePath)
if err != nil {
return []string{}, err
}
ignore := strings.Split(string(contents), "\n")
result := []string{}
for _, prefix := range ignore {
ignoreStr := strings.TrimSpace(prefix)
if ignoreStr != "" {
result = append(result, ignoreStr)
}
}
return result, nil
}
func DefaultDiff(path string, prev *Diff) (e *Diff) {
byName := map[string]*executor.Step{}
steps := defaultDiff(path)
for _, step := range prev.Steps {
byName[step.Name] = step
}
for _, step := range steps {
prev, ok := byName[step.Name]
if ok {
step.Sha = prev.Sha
}
byName[step.Name] = step
}
// set up a topsort between the two orders of operations
graph := containers.NewGraph[string]()
for i := 0; i < len(steps)-1; i++ {
graph.AddEdge(steps[i].Name, steps[i+1].Name)
}
for i := 0; i < len(prev.Steps)-1; i++ {
graph.AddEdge(prev.Steps[i].Name, prev.Steps[i+1].Name)
}
sorted, _ := algorithms.TopsortGraph(graph)
finalizedSteps := algorithms.Map(sorted, func(s string) *executor.Step { return byName[s] })
return &Diff{
Metadata: Metadata{Path: path, Name: "diff"},
Steps: finalizedSteps,
}
}
func (d *Diff) Flush(root string) error {
io, err := hclencoder.Encode(&d)
if err != nil {
return err
}
path, _ := filepath.Abs(pathing.SanitizeFilepath(filepath.Join(root, d.Metadata.Path, d.Metadata.Name+".hcl")))
return os.WriteFile(path, io, 0644)
}