-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (78 loc) · 1.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
var usage = func() {
fmt.Printf("Usage: renumber_proto [path to proto directory]")
flag.PrintDefaults()
}
func main() {
// validate arguments
flag.Usage = usage
if len(os.Args) < 2 {
flag.Usage()
os.Exit(0)
}
protoDir := os.Args[1]
// validate directory path
info, err := os.Stat(protoDir)
if os.IsNotExist(err) {
log.Fatalf("Directory does not exist: %s", protoDir)
}
if !info.IsDir() {
log.Fatalf("Path is not a directory: %s", protoDir)
}
// walk the directory
err = filepath.Walk(protoDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".proto") {
log.Println(path)
var rawProto []byte
if rawProto, err = os.ReadFile(path); err != nil {
return err
}
var (
content = string(rawProto)
renumberedAll = renumberAllMessages(content)
)
if err = os.WriteFile(path, []byte(renumberedAll), 0644); err != nil {
return err
}
}
return nil
})
if err != nil {
log.Fatalf("Failed to walk directory: %s", err)
}
}
func renumberAllMessages(text string) string {
var messages = regexp.MustCompile(`(?sm)^message.*?{$.*?^}`).FindAllString(text, -1)
for _, msg := range messages {
renumbered := renumberFields(msg)
text = strings.ReplaceAll(text, msg, renumbered)
}
return text
}
func renumberFields(text string) string {
var (
loc = regexp.MustCompile(`(?sm){$.*^}`).FindStringIndex(text)
body = text[loc[0]:loc[1]]
i = 1
replaced = regexp.MustCompile(`(?sm)\d+;$`).
ReplaceAllStringFunc(body, func(s string) string {
s = fmt.Sprintf("%d;", i)
i++
return s
})
)
return strings.ReplaceAll(text, body, replaced)
}