-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (121 loc) · 3.22 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
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
// UUIDRegex is the regex to find UUIDs
const UUIDRegex = `([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})`
// TrUUIDRegex is the simple regex to find trimmed UUIDs
const TrUUIDRegex = `([0-9a-f]{32})`
func main() {
// yey flags
uuidP := flag.String("uuid", "", "UUID to convert")
helpP := flag.Bool("help", false, "help?")
fileP := flag.String("file", "", "Specify a file to change")
dirP := flag.String("dir", ".", "Directory to scan for files")
extP := flag.String("ext", "", "Only scan these extensions")
recursiveP := flag.Bool("r", false, "Scan directory recursive")
simulateP := flag.Bool("simulate", false, "Don't change files, only simulate the process")
flag.Parse()
if *helpP {
println("this unholy place has no help")
return
}
// do have to deal with arguments?
if len(os.Args) > 1 {
var r *regexp.Regexp
var uuid *string = &os.Args[1]
if *uuidP != "" {
uuid = uuidP
}
if matched, _ := regexp.MatchString(UUIDRegex, *uuid); matched {
r = regexp.MustCompile(UUIDRegex) // UUID
} else if matched, _ := regexp.MatchString(TrUUIDRegex, *uuid); matched {
r = regexp.MustCompile(TrUUIDRegex) // Trimmed UUID
}
if r != nil {
// return the converted UUID
intArray, _ := convertUUID(r.FindStringSubmatch(*uuid)[0])
fmt.Println(stringifyArray(intArray))
return
}
}
if *fileP != "" {
alterFile(*fileP, *simulateP)
} else {
// just walk through all files and alter them
err := filepath.Walk(*dirP, func(path string, info os.FileInfo, err error) error {
if info.Name() == ".git" || (!*recursiveP && path != "." && info.IsDir()) {
return filepath.SkipDir
}
if info.IsDir() || filepath.Ext(path) == ".exe" {
return nil
}
if *extP == "" || filepath.Ext(path) == *extP {
alterFile(path, *simulateP)
}
return nil
})
if err != nil {
panic(err)
}
}
}
func alterFile(file string, simulate bool) {
re := regexp.MustCompile(UUIDRegex)
rFile, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
return
}
// skip file if no match was found
if len(re.FindSubmatch(rFile)) < 2 {
fmt.Println(file + " skipped.")
return
}
// replace UUID with int_array format
rFile = re.ReplaceAllFunc(rFile, func(match []byte) []byte {
intArray, _ := convertUUID(string(re.FindSubmatch(match)[1]))
return []byte(stringifyArray(intArray))
})
fmt.Println(file + " updated.")
if !simulate {
// your original file is now rip
ioutil.WriteFile(file, rFile, 0666)
}
}
func stringifyArray(uuid [4]int32) string {
parsed := "[I;"
for i, n := range uuid {
parsed += fmt.Sprintf("%d", n)
if i < len(uuid)-1 {
parsed += ","
}
}
return parsed + "]"
}
func convertUUID(uuid string) ([4]int32, error) {
// remove "-"
uuid = strings.ReplaceAll(uuid, "-", "")
var uuidIntArray [4]int32
// for real, why?
if len(uuid) != 32 {
return uuidIntArray, errors.New(uuid + " is not a valid UUID")
}
for i := 0; i < 4; i++ {
slice := uuid[i*8 : i*8+8]
// convert hex to int64 because stupid
n, _ := strconv.ParseInt(slice, 16, 64)
// convert int64 to int32 because stupid
uuidIntArray[i] = int32(n)
}
return uuidIntArray, nil
}