This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
/
pipeline.go
107 lines (94 loc) · 2.03 KB
/
pipeline.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
package scan
import (
"strings"
. "github.com/dprotaso/go-yit"
"gopkg.in/yaml.v3"
)
func SearchImageNodes(doc *yaml.Node) []*yaml.Node {
return FromIterators(
MatchImageKey(doc),
MatchArgsMap(doc),
MatchTemplateDefaults(doc),
).ToArray()
}
func MatchImageKey(node *yaml.Node) Iterator {
return FromNode(node).
RecurseNodes().
Filter(WithKind(yaml.MappingNode)).
ValuesForMap(
// Key Predicate
Union(
WithValue("image"),
WithSuffix("Image"),
WithSuffix("-image"),
),
// Value Predicate
StringValue,
).
Filter(Negate(WithPrefix("$")))
}
func MatchTemplateDefaults(node *yaml.Node) Iterator {
return FromNode(node).
RecurseNodes().
Filter(WithKind(yaml.MappingNode)).
ValuesForMap(
// Key Predicate
WithValue("parameters"),
// Value Prediate
WithKind(yaml.SequenceNode),
).
Values(). // Unpack sequences
Filter(
Intersect(
WithKind(yaml.MappingNode),
WithMapKeyValue(WithValue("name"), WithSuffix("IMAGE")),
WithMapKey("default"),
),
).
ValuesForMap(
// Key Predicate
WithValue("default"),
// Value Predicate
StringValue,
)
}
func MatchArgsMap(node *yaml.Node) Iterator {
return FromNode(node).
RecurseNodes().
Filter(WithKind(yaml.MappingNode)).
ValuesForMap(
// Key Predicate
WithValue("args"),
// Value Predicate
WithKind(yaml.SequenceNode),
).
Iterate(imageFlagValues).
Filter(StringValue)
}
var imageFlagValues = func(next Iterator) Iterator {
var content []*yaml.Node
return func() (node *yaml.Node, ok bool) {
for {
for len(content) >= 2 {
arg := content[0]
node = content[1]
// not all arg sequences are (flag, value) pairs
content = content[1:]
if ok = strings.HasPrefix(arg.Value, "-") &&
strings.HasSuffix(arg.Value, "-image"); ok {
return
}
}
var parent *yaml.Node
for parent, ok = next(); ok; parent, ok = next() {
if parent.Kind == yaml.SequenceNode && len(parent.Content) > 0 {
break
}
}
if !ok {
return
}
content = parent.Content
}
}
}