-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotator.go
258 lines (232 loc) · 5.64 KB
/
annotator.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
)
type manifestAnnotator struct {
FileName string
Annotation string
SkipAnnotation string
Value string
Kind string
GroupVersion string
Name string
Namespace string
}
type manifest struct {
name string
namespace string
start int
end int
}
func (a *manifestAnnotator) Run() error {
lines, err := readLines(a.FileName)
if err != nil {
return err
}
output := &bytes.Buffer{}
currentManifest := []string{}
for _, line := range lines {
if strings.HasPrefix(line, "---") {
a.processManifest(currentManifest, output)
currentManifest = []string{}
} else {
currentManifest = append(currentManifest, line)
continue
}
output.WriteString(line + "\n")
}
changed := a.processManifest(currentManifest, output)
if changed {
if err = ioutil.WriteFile(a.FileName, output.Bytes(), 0644); err != nil {
return err
}
}
return nil
}
func (a *manifestAnnotator) processManifest(lines []string, out *bytes.Buffer) bool {
changed := false
// first determine kind and apiVersion
var kind, groupVersion string
for _, line := range lines {
if strings.HasPrefix(line, "kind:") {
kind = strings.TrimSpace(strings.TrimPrefix(line, "kind:"))
continue
}
if strings.HasPrefix(line, "apiVersion:") {
groupVersion = strings.TrimSpace(strings.TrimPrefix(line, "apiVersion:"))
}
}
inMetadata := false
metadataLines := []string{}
metadataProcessed := false
for _, line := range lines {
if inMetadata {
if !strings.HasPrefix(line, " ") {
changed = a.processMetadata(metadataLines, kind, groupVersion, out)
inMetadata = false
metadataProcessed = true
} else {
metadataLines = append(metadataLines, line)
continue
}
}
if strings.HasPrefix(line, "metadata:") {
inMetadata = true
}
out.WriteString(line + "\n")
}
if !metadataProcessed && len(metadataLines) > 0 {
changed = a.processMetadata(metadataLines, kind, groupVersion, out)
}
return changed
}
func (a *manifestAnnotator) processMetadata(lines []string, kind, groupVersion string, out *bytes.Buffer) bool {
// Determine information about the current manifest
changed := false
var name, namespace string
for _, line := range lines {
if strings.HasPrefix(line, " name:") {
name = strings.TrimSpace(strings.TrimPrefix(line, " name:"))
}
if strings.HasPrefix(line, " namespace:") {
namespace = strings.TrimSpace(strings.TrimPrefix(line, " namespace:"))
}
}
skipProcessing := (len(a.Kind) > 0 && a.Kind != kind) ||
(len(a.GroupVersion) > 0 && a.GroupVersion != groupVersion) ||
(len(a.Name) > 0 && a.Name != name) ||
(len(a.Namespace) > 0 && a.Namespace != namespace)
if skipProcessing {
for _, line := range lines {
out.WriteString(line + "\n")
}
return changed
}
annotationLines := []string{}
inAnnotations := false
annotationsProcessed := false
for _, line := range lines {
if inAnnotations {
if !strings.HasPrefix(line, " ") {
changed = a.processAnnotations(annotationLines, out)
inAnnotations = false
annotationsProcessed = true
} else {
annotationLines = append(annotationLines, line)
continue
}
}
if strings.HasPrefix(line, " annotations:") {
inAnnotations = true
}
out.WriteString(line + "\n")
}
if len(annotationLines) == 0 { // annotations were never found
changed = true
out.WriteString(" annotations:\n")
}
if !annotationsProcessed {
changed = a.processAnnotations(annotationLines, out)
}
return changed
}
func (a *manifestAnnotator) processAnnotations(lines []string, out *bytes.Buffer) bool {
annotations := parseAnnotations(lines)
if !annotations.Includes(a.Annotation) && !annotations.Includes(a.SkipAnnotation) {
annotations.Add(a.Annotation, a.Value)
annotations.Sort()
annotations.Write(out)
return true
}
annotations.Write(out)
return false
}
type annotation struct {
key string
lines []string
}
type annotations []annotation
func (a annotations) Len() int {
return len(a)
}
func (a annotations) Less(i, j int) bool {
return a[i].key < a[j].key
}
func (a annotations) Swap(i, j int) {
tmp := a[i]
a[i] = a[j]
a[j] = tmp
}
func (a annotations) Sort() {
sort.Sort(a)
}
func (a annotations) Includes(key string) bool {
for _, aa := range a {
if aa.key == key {
return true
}
}
return false
}
func (a annotations) Write(out *bytes.Buffer) {
for _, aa := range a {
for _, line := range aa.lines {
out.WriteString(line + "\n")
}
}
}
func (a *annotations) Add(key, value string) {
*a = append(*a, newAnnotation(key, value))
}
func newAnnotation(key, value string) annotation {
return annotation{
key: key,
lines: []string{fmt.Sprintf(" %s: %s", key, value)},
}
}
func parseAnnotations(lines []string) annotations {
var currentAnnotation *annotation
result := annotations{}
for _, line := range lines {
if strings.HasPrefix(line, " ") {
if currentAnnotation != nil {
currentAnnotation.lines = append(currentAnnotation.lines, line)
}
continue
}
parts := strings.SplitN(line, ":", 2)
if len(parts) < 2 {
continue
}
if currentAnnotation != nil {
result = append(result, *currentAnnotation)
}
currentAnnotation = &annotation{
key: strings.TrimSpace(parts[0]),
lines: []string{line},
}
}
if currentAnnotation != nil {
result = append(result, *currentAnnotation)
}
return result
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}