-
Notifications
You must be signed in to change notification settings - Fork 30
/
modify.go
60 lines (52 loc) · 1.34 KB
/
modify.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
package kyamls
import (
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// ModifyFiles recursively walks the given directory and modifies any suitable file
func ModifyFiles(dir string, modifyFn func(node *yaml.RNode, path string) (bool, error), filter Filter) error {
filterFn, err := filter.ToFilterFn()
if err != nil {
return errors.Wrap(err, "failed to create filter")
}
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".yaml") && !strings.HasSuffix(path, ".yml") {
return nil
}
node, err := yaml.ReadFile(path)
if err != nil {
return errors.Wrapf(err, "failed to load file %s", path)
}
if filterFn != nil {
flag, err := filterFn(node, path)
if err != nil {
return errors.Wrapf(err, "failed to evaluate filter on file %s", path)
}
if !flag {
return nil
}
}
modified, err := modifyFn(node, path)
if err != nil {
return errors.Wrapf(err, "failed to modify file %s", path)
}
if !modified {
return nil
}
err = yaml.WriteFile(node, path)
if err != nil {
return errors.Wrapf(err, "failed to save %s", path)
}
return nil
})
if err != nil {
return errors.Wrapf(err, "failed to modify files in dir %s", dir)
}
return nil
}