-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (110 loc) · 2.81 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"gopkg.in/yaml.v3"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
var bin = os.Args[0]
func run() error {
flag.Usage = func() {
fmt.Println(bin)
fmt.Println()
fmt.Println("description: convert all yaml files within target folder to use tag locks instead of comment locks")
fmt.Println()
fmt.Printf("usage: %s $TARGET_FOLDER", bin)
}
flag.Parse()
targetFolder := func() string {
if arg := flag.Arg(0); arg != "" {
return arg
}
return "."
}()
return filepath.WalkDir(targetFolder, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
if isHidden(path) {
return filepath.SkipDir
}
return nil
}
if ext := filepath.Ext(path); ext != ".yml" && ext != ".yaml" {
return nil
}
if err := RewriteYAML(path); err != nil {
return fmt.Errorf("failed to rewrite %s: %v", path, err)
}
return nil
})
}
func RewriteYAML(name string) (err error) {
data, err := os.ReadFile(name)
if err != nil {
return err
}
var node Node
if err := yaml.Unmarshal(data, &node); err != nil {
return err
}
rewriteLocks(node.Node)
file, err := os.Create(name)
if err != nil {
return err
}
defer func() { err = errors.Join(file.Close()) }()
encoder := yaml.NewEncoder(file)
encoder.SetIndent(2)
return encoder.Encode(node)
}
func rewriteLocks(node *yaml.Node) {
if IsCommentLocked(node) {
node.Tag = "!lock"
node.LineComment = lockExpression.ReplaceAllString(node.LineComment, "")
node.HeadComment = lockExpression.ReplaceAllString(node.LineComment, "")
}
if node.Kind == yaml.MappingNode {
for i := 0; i < len(node.Content); i += 2 {
key, value := node.Content[i], node.Content[i+1]
if IsCommentLocked(key) || IsCommentLocked(value) {
value.Tag = "!lock"
key.HeadComment = lockExpression.ReplaceAllString(key.HeadComment, "")
value.LineComment = lockExpression.ReplaceAllString(value.LineComment, "")
}
}
}
for _, child := range node.Content {
rewriteLocks(child)
}
}
// The node type is a wrapper over a yaml.Node. The reason for this wrapper is to make Marshallinga and Unmarshalling easier.
// go-yaml fails to unmarshal document nodes, and we always end up needing to go between the document and the first content item.
// With this we store the first unmashalled node which is the content not the document.
type Node struct {
*yaml.Node
}
func (n *Node) UnmarshalYAML(node *yaml.Node) error {
n.Node = node
return nil
}
func (n Node) MarshalYAML() (any, error) {
return n.Node, nil
}
var lockExpression = regexp.MustCompile(`(?i)(?m)^#+\s*lock\s*\n?`)
func isHidden(name string) bool {
base := filepath.Base(name)
return base != "." && strings.HasSuffix(base, ".")
}