-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
182 lines (161 loc) · 4.25 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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
)
// regexp
var (
regBranchIdNumeric = []*regexp.Regexp{
// cs, go, py
regexp.MustCompile("(.*[bB][rR][aA][nN][cC][hH]_?[iI][dD]\\s*=\\s*)(.\\d*)(.*)"),
// json
regexp.MustCompile("(.*\"[bB][rR][aA][nN][cC][hH]_?[iI][dD]\"\\s*:\\s*)(.\\d*)(.*)"),
// c
regexp.MustCompile("(#define [bB][rR][aA][nN][cC][hH]_?[iI][dD]\\s*)(.\\d*)(.*)"),
}
regBranchIdStr = []*regexp.Regexp{
// cs, go, py
regexp.MustCompile("(.*[bB][rR][aA][nN][cC][hH]_?[iI][dD]\\s*=\\s*[\"'])(.*)([\"'].*)"),
// json
regexp.MustCompile("(.*\"[bB][rR][aA][nN][cC][hH]_?[iI][dD]\"\\s*:\\s*\")(.*)(\".*)"),
// c
regexp.MustCompile("(#define [bB][rR][aA][nN][cC][hH]_?[iI][dD]\\s*\")(.*)(\".*)"),
}
regBranchName = []*regexp.Regexp{
// cs, go, py
regexp.MustCompile("(.*[bB][rR][aA][nN][cC][hH]_?[nN][aA][mM][eE]\\s*=\\s*[\"'])(.*)([\"'].*)"),
// json
regexp.MustCompile("(.*\"[bB][rR][aA][nN][cC][hH]_?[nN][aA][mM][eE]\"\\s*:\\s*\")(.*)(\".*)"),
}
regBuildId = []*regexp.Regexp{
// cs, go, py
regexp.MustCompile("(.*[bB][uU][iI][lL][dD]_?[iI][dD]\\s*=\\s*)(\\d*)(.*)"),
// json
regexp.MustCompile("(.*\"[bB][uU][iI][lL][dD]_?[iI][dD]\"\\s*:\\s*)(\\d*)(.*)"),
// c
regexp.MustCompile("(#define [bB][uU][iI][lL][dD]_?[iI][dD]\\s*)(\\d*)(.*)"),
}
regBuildConfig = []*regexp.Regexp{
// cs, go, py
regexp.MustCompile("(.*[bB][uU][iI][lL][dD]_?[cC][oO][nN][fF][iI][gG]\\s*=\\s*[\"'])(.*)([\"'].*)"),
// json
regexp.MustCompile("(.*\"[bB][uU][iI][lL][dD]_?[cC][oO][nN][fF][iI][gG]\"\\s*:\\s*\")(.*)(\".*)"),
// c
regexp.MustCompile("(#define [bB][uU][iI][lL][dD]_?[cC][oO][nN][fF][iI][gG]\\s*\")(.*)(\".*)"),
}
)
// config vars
var (
path string
branch string
branchId string
buildConfig string
buildId int
useStringBranchId bool
)
func main() {
parseArgs()
labelBuild()
}
func labelBuild() {
var buffer bytes.Buffer
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
reader := bufio.NewReader(f)
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
replaced := false
newLine := line
if useStringBranchId {
replaced, newLine = replaceInfo(
regBranchIdStr,
line,
fmt.Sprintf("${1}%s${3}", branchId),
)
if replaced {
buffer.WriteString(newLine)
continue
}
} else {
replaced, newLine = replaceInfo(
regBranchIdNumeric,
line,
fmt.Sprintf("${1}%s${3}", branchId),
)
if replaced {
buffer.WriteString(newLine)
continue
}
}
replaced, newLine = replaceInfo(
regBranchName,
line,
fmt.Sprintf("${1}%s${3}", branch),
)
if replaced {
buffer.WriteString(newLine)
continue
}
replaced, newLine = replaceInfo(
regBuildConfig,
line,
fmt.Sprintf("${1}%s${3}", buildConfig),
)
if replaced {
buffer.WriteString(newLine)
continue
}
replaced, newLine = replaceInfo(
regBuildId,
line,
fmt.Sprintf("${1}%d${3}", buildId),
)
if replaced {
buffer.WriteString(newLine)
continue
}
// write line as-is
buffer.WriteString(line)
}
f.Close()
ioutil.WriteFile(path, buffer.Bytes(), 0660)
}
func replaceInfo(regexList []*regexp.Regexp, line, repl string) (bool, string) {
for _, regex := range regexList {
if len(regex.FindStringSubmatch(line)) == 4 {
return true, regex.ReplaceAllString(line, repl)
}
}
return false, ""
}
func parseArgs() {
flag.StringVar(&path, "path", "", "(required) Path to the source file containing build information")
flag.StringVar(&branch, "branchName", "", "(required) The branch to label the source file with")
flag.StringVar(&branchId, "branchId", "-1", "(required) The numeric branch id to label the source file with")
flag.StringVar(&buildConfig, "buildConfig", "Debug", "The build configuration to label the source file with")
flag.IntVar(&buildId, "buildId", 0, "The BuildID to label the source file with")
flag.BoolVar(&useStringBranchId, "useStringBranchId", false, "Whether to interpret branch id as a string or number")
flag.Parse()
if useStringBranchId {
fmt.Println("Using string branch IDs")
}
if len(path) < 1 || len(branch) < 1 || branchId == "-1" {
flag.PrintDefaults()
os.Exit(1)
}
}