-
Notifications
You must be signed in to change notification settings - Fork 50
/
editor.go
160 lines (145 loc) · 3.55 KB
/
editor.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
package helmfiles
import (
"os"
"path/filepath"
"sort"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-helpers/v3/pkg/yaml2s"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/roboll/helmfile/pkg/state"
)
var (
info = termcolor.ColorInfo
)
// Editor an editor of helmfiles
type Editor struct {
pathToState map[string]*state.HelmState
namespaceToPath map[string]string
modified map[string]bool
dir string
helmfiles []Helmfile
}
// NewEditor creates a new editor
func NewEditor(dir string, helmfiles []Helmfile) (*Editor, error) {
e := &Editor{
pathToState: map[string]*state.HelmState{},
namespaceToPath: map[string]string{},
modified: map[string]bool{},
dir: dir,
helmfiles: helmfiles,
}
for i := range helmfiles {
src := &helmfiles[i]
helmState := &state.HelmState{}
path := src.Filepath
err := yaml2s.LoadFile(path, helmState)
if err != nil {
return nil, errors.Wrapf(err, "failed to load helmfile %s", path)
}
e.pathToState[path] = helmState
if helmState.OverrideNamespace != "" {
e.namespaceToPath[helmState.OverrideNamespace] = path
}
}
return e, nil
}
func (e *Editor) getOrCreateState(path string) *state.HelmState {
hf := e.pathToState[path]
if hf == nil {
hf = &state.HelmState{}
e.pathToState[path] = hf
}
return hf
}
// AddChart adds a chart to the right helmfile for the given namespace
func (e *Editor) AddChart(opts *ChartDetails) error {
ns := opts.Namespace
if ns == "" {
return errors.Errorf("no namespace")
}
rel := filepath.Join("helmfiles", ns, "helmfile.yaml")
path := e.namespaceToPath[ns]
if path == "" {
// lets create a new path
path = filepath.Join(e.dir, rel)
e.namespaceToPath[ns] = path
}
hf := e.getOrCreateState(path)
hf.OverrideNamespace = ns
rootPath := e.helmfiles[0].Filepath
root := e.getOrCreateState(rootPath)
found := false
for _, f := range root.Helmfiles {
if f.Path == rel {
found = true
break
}
}
if !found {
root.Helmfiles = append(root.Helmfiles, state.SubHelmfileSpec{
Path: rel,
})
sort.Slice(root.Helmfiles, func(i, j int) bool {
h1 := root.Helmfiles[i]
h2 := root.Helmfiles[j]
return h1.Path < h2.Path
})
e.modified[rootPath] = true
}
modified, err := opts.Add(hf)
if err != nil {
return errors.Wrapf(err, "failed to add chart")
}
if modified {
e.modified[path] = true
}
return nil
}
// DeleteChart adds a chart to the right helmfile for the given namespace
func (e *Editor) DeleteChart(opts *ChartDetails) error {
for ns, path := range e.namespaceToPath {
if path == "" {
continue
}
if opts.Namespace != "" && opts.Namespace != ns {
continue
}
hf := e.pathToState[path]
if hf == nil {
continue
}
modified, err := opts.Delete(hf)
if err != nil {
return errors.Wrapf(err, "failed to add chart")
}
if modified {
e.modified[path] = true
}
}
return nil
}
// Save saves any modified files
func (e *Editor) Save() error {
for path, f := range e.modified {
if !f {
continue
}
state := e.pathToState[path]
if state == nil {
return errors.Errorf("no state for path %s", path)
}
dir := filepath.Dir(path)
err := os.MkdirAll(dir, files.DefaultDirWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to make dir %s", dir)
}
err = yaml2s.SaveFile(state, path)
if err != nil {
return errors.Wrapf(err, "failed to save file %s", path)
}
log.Logger().Infof("saved %s", info(path))
}
return nil
}