-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (162 loc) · 3.93 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bytes"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"sync"
"unicode/utf8"
"unsafe"
"github.com/heliaxdev/namada-address-migrations/namada"
"github.com/heliaxdev/namada-address-migrations/namada/addrconv"
)
type addressReplace struct {
old []byte
new []byte
}
var replacePool = sync.Pool{
New: func() any {
return make([]addressReplace, 0, 16)
},
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}
func run() error {
if len(os.Args) < 2 {
return dieUsage()
}
switch os.Args[1] {
case "h", "help", "u", "usage":
usage(os.Stdout)
return nil
case "c", "convert":
if len(os.Args) != 3 {
return dieUsage()
}
return convert(os.Args[2])
case "r", "replace":
if len(os.Args) != 3 {
return dieUsage()
}
return replace(os.Args[2])
case "i", "implicit-address":
if len(os.Args) != 3 {
return dieUsage()
}
return implicitAddress(os.Args[2])
}
return dieUsage()
}
func replace(rootPath string) error {
wg := &sync.WaitGroup{}
semaphore := make(chan struct{}, runtime.NumCPU()<<1)
runFindReplace(wg, semaphore, rootPath)
wg.Wait()
return nil
}
func runFindReplace(wg *sync.WaitGroup, sem chan struct{}, rootPath string) {
filepath.WalkDir(rootPath, func(path string, ent fs.DirEntry, err error) error {
sem <- struct{}{}
wg.Add(1)
go func() {
defer func() {
<-sem
wg.Done()
}()
findReplace(path, ent)
}()
return nil
})
}
func findReplace(path string, ent fs.DirEntry) {
// 1 mib
const sizeThreshold = 1 << 20
if ent.Type()&fs.ModeType != 0 {
return
}
info, err := ent.Info()
if err != nil {
fmt.Fprintf(os.Stderr, "error: failed read file info: %s: %s\n", path, err)
return
}
if info.Size() >= sizeThreshold {
return
}
buf, err := os.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "error: failed to read file: %s: %s\n", path, err)
return
}
if !utf8.Valid(buf) {
return
}
occurrences := replacePool.Get().([]addressReplace)
for _, match := range namada.AddressRegex.FindAll(buf, -1) {
oldAddress := *(*string)(unsafe.Pointer(&match))
newAddress, err := addrconv.ConvertAddress(oldAddress)
if err != nil {
fmt.Fprintf(os.Stderr, "error: failed to convert address: %s: %s\n", oldAddress, err)
continue
}
occurrences = append(occurrences, addressReplace{
old: bytes.Clone(match),
new: []byte(newAddress),
})
}
for i := 0; i < len(occurrences); i++ {
occurrence := &occurrences[i]
buf = bytes.ReplaceAll(buf, occurrence.old, occurrence.new)
}
occurrences = occurrences[:0]
replacePool.Put(occurrences)
err = os.WriteFile(path, buf, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "error: failed to rewrite to file: %s: %s\n", path, err)
return
}
}
func convert(oldAddr string) error {
newAddr, err := addrconv.ConvertAddress(oldAddr)
if err != nil {
return err
}
fmt.Println(newAddr)
return nil
}
func implicitAddress(bech32PublicKey string) error {
addr, err := addrconv.PublicKeyToImplicit(bech32PublicKey)
if err != nil {
return err
}
fmt.Println(addr)
return nil
}
func dieUsage() error {
usage(os.Stderr)
return fmt.Errorf("invalid cli arguments detected")
}
func usage(f *os.File) {
fmt.Fprintf(f, "usage: %s <args>, where <args> =\n", os.Args[0])
fmt.Fprintln(f)
fmt.Fprintf(f, "\t# show this help message\n")
fmt.Fprintf(f, "\thelp|usage|h|u\n")
fmt.Fprintln(f)
fmt.Fprintf(f, "\t# convert from the old to the new addr fmt,\n")
fmt.Fprintf(f, "\t# printing the new address to stdout\n")
fmt.Fprintf(f, "\tc|convert <old-address>\n")
fmt.Fprintln(f)
fmt.Fprintf(f, "\t# get an implicit address from a public key\n")
fmt.Fprintf(f, "\ti|implicit-address <bech32-encoded-pk>\n")
fmt.Fprintln(f)
fmt.Fprintf(f, "\t# replace occurrences of old addresses with\n")
fmt.Fprintf(f, "\t# the new address format in all files present\n")
fmt.Fprintf(f, "\t# in the given path\n")
fmt.Fprintf(f, "\tr|replace <path>\n")
fmt.Fprintln(f)
}