...
1
2
3
4 package filtersutil
5
6 import (
7 "sigs.k8s.io/kustomize/kyaml/yaml"
8 )
9
10
11 type SetFn func(*yaml.RNode) error
12
13
14 func SetScalar(value string) SetFn {
15 return SetEntry("", value, yaml.NodeTagEmpty)
16 }
17
18
19
20
21 func SetEntry(name, value, tag string) SetFn {
22 n := &yaml.Node{
23 Kind: yaml.ScalarNode,
24 Value: value,
25 Tag: tag,
26 }
27 return func(node *yaml.RNode) error {
28 return node.PipeE(yaml.FieldSetter{
29 Name: name,
30 Value: yaml.NewRNode(n),
31 })
32 }
33 }
34
35 type TrackableSetter struct {
36
37 setValueCallback func(name, value, tag string, node *yaml.RNode)
38 }
39
40
41 func (s *TrackableSetter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) *TrackableSetter {
42 s.setValueCallback = callback
43 return s
44 }
45
46
47
48
49 func (s TrackableSetter) SetScalar(value string) SetFn {
50 return s.SetEntry("", value, yaml.NodeTagEmpty)
51 }
52
53
54
55
56 func (s TrackableSetter) SetScalarIfEmpty(value string) SetFn {
57 return s.SetEntryIfEmpty("", value, yaml.NodeTagEmpty)
58 }
59
60
61
62
63
64
65 func (s TrackableSetter) SetEntry(name, value, tag string) SetFn {
66 origSetEntry := SetEntry(name, value, tag)
67 return func(node *yaml.RNode) error {
68 if s.setValueCallback != nil {
69 s.setValueCallback(name, value, tag, node)
70 }
71 return origSetEntry(node)
72 }
73 }
74
75
76
77
78
79
80 func (s TrackableSetter) SetEntryIfEmpty(key, value, tag string) SetFn {
81 origSetEntry := SetEntry(key, value, tag)
82 return func(node *yaml.RNode) error {
83 if hasExistingValue(node, key) {
84 return nil
85 }
86 if s.setValueCallback != nil {
87 s.setValueCallback(key, value, tag, node)
88 }
89 return origSetEntry(node)
90 }
91 }
92
93 func hasExistingValue(node *yaml.RNode, key string) bool {
94 if node.IsNilOrEmpty() {
95 return false
96 }
97 if err := yaml.ErrorIfInvalid(node, yaml.ScalarNode); err == nil {
98 return yaml.GetValue(node) != ""
99 }
100 entry := node.Field(key)
101 if entry.IsNilOrEmpty() {
102 return false
103 }
104 return yaml.GetValue(entry.Value) != ""
105 }
106
View as plain text