-
Notifications
You must be signed in to change notification settings - Fork 128
/
chart.go
175 lines (146 loc) · 3.88 KB
/
chart.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
167
168
169
170
171
172
173
174
175
package helm
import (
"fmt"
"os"
"path/filepath"
"regexp"
"gopkg.in/yaml.v2"
)
// HelmChart data structure based on map slice
type HelmChart yaml.MapSlice
// Read reads helm chart based on path
func Read(filePath string) (helmChart HelmChart, err error) {
content, err := os.ReadFile(filePath)
if err != nil {
return helmChart, err
}
err = yaml.Unmarshal(content, &helmChart)
return
}
// Write writes content of HelmChart to given path
func Write(filePath string, helmChart HelmChart) (err error) {
content, err := yaml.Marshal(helmChart)
if err != nil {
return err
}
return os.WriteFile(filePath, content, 0644)
}
// UpdateDependencyVersion updates version in HelmChart
func UpdateDependencyVersion(in HelmChart, dependency, version string) (out HelmChart, err error) {
// go through SliceMap
for ci, i := range in {
if i.Key == "dependencies" {
deps, ok := i.Value.([]interface{})
if !ok {
return out, fmt.Errorf("dependencies key is not array")
}
for di, idep := range deps {
fields, ok := idep.(HelmChart)
if !ok {
return out, fmt.Errorf("invalid dependencies key values")
}
for _, f := range fields {
if f.Key == "name" && f.Value == dependency {
for fi, f := range fields {
if f.Key == "version" {
in[ci].Value.([]interface{})[di].(HelmChart)[fi].Value = version
return in, nil
}
}
}
}
}
}
}
return out, fmt.Errorf("dependency not found")
}
// GetDependencyVersion returns selected helm chart dependency version
func GetDependencyVersion(helmChart HelmChart, dependency string) (string, error) {
for _, i := range helmChart {
if i.Key == "dependencies" {
deps, ok := i.Value.([]interface{})
if !ok {
return "", fmt.Errorf("dependencies key is not array")
}
for _, ifields := range deps {
fields, ok := ifields.(HelmChart)
if !ok {
return "", fmt.Errorf("invalid dependencies key values")
}
for _, f := range fields {
if f.Key == "name" && f.Value == dependency {
for _, f := range fields {
if f.Key == "version" {
return f.Value.(string), nil
}
}
}
}
}
}
}
return "", fmt.Errorf("version key not found in dependency " + dependency)
}
// GetVersion returns HelmChart version
func GetVersion(helmChart HelmChart) string {
// go through SliceMap
for _, i := range helmChart {
if i.Key == "version" {
val, ok := i.Value.(string)
if ok {
return val
}
}
}
return "0.0.0"
}
// SaveString saves given key with passed value in HelmChart
func SaveString(helmChart *HelmChart, key, value string) error {
for k := range *helmChart {
if (*helmChart)[k].Key == key {
(*helmChart)[k].Value = value
return nil
}
}
return fmt.Errorf("key %s not found in %+v", key, helmChart)
}
// GetChart returns Chart based on directory, locates chart automatically
func GetChart(dir string) (helmChart HelmChart, chartPath string, err error) {
chartPath, err = Find(dir)
if err != nil {
return helmChart, chartPath, err
}
helmChart, err = Read(chartPath)
return helmChart, chartPath, err
}
// Find locates helm chart in given dir
func Find(dir string) (chartPath string, err error) {
dirInfo, err := os.Stat(dir)
if err != nil {
return "", err
}
if !dirInfo.IsDir() {
return "", fmt.Errorf("passed '%s' path is not directory", dir)
}
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if info.Name() == "Chart.yaml" {
chartPath = path
return filepath.SkipDir
}
return nil
})
return
}
// UpdateValuesImageTag updates values.yaml image tag field
func UpdateValuesImageTag(path, tag string) error {
input, err := os.ReadFile(path)
if err != nil {
return err
}
r := regexp.MustCompile(`tag: "[^"]+"`)
output := r.ReplaceAll(input, []byte(fmt.Sprintf(`tag: "%s"`, tag)))
return os.WriteFile(path, output, 0644)
}