-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanse-go-mods
executable file
·71 lines (60 loc) · 2.68 KB
/
cleanse-go-mods
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
#!/usr/bin/env python3
# Steve Willoughby <[email protected]>
#
cur_agent_version='3.29.0'
minimum_go_version='1.19'
import os
import os.path
import re
for root, dirs, files in os.walk('.'):
for f in files:
if f == 'go.mod':
target = os.path.join(root, f)
os.rename(target, target+'.bak')
with open(target+'.bak') as src:
with open(target, 'w') as dst:
block = hold = False
replace_line = False
for line in src:
if not line.strip():
dst.write('\n')
continue
if line.strip() == 'require (':
hold=True
block=True
continue
if line.strip() == ')':
if block and hold:
block = hold = False
continue
block = hold = False
if '// indirect' in line:
continue
if block:
if hold:
dst.write('require (\n')
hold = False
if re.search(r'^.*go\s+\d+\.\d+', line):
dst.write(f'go {minimum_go_version}\n')
continue
if re.search(r'newrelic/go-agent/v3\s+v(\d+\.\d+\.\d+)', line):
line = re.sub(r'newrelic/go-agent/v3\s+v(\d+\.\d+\.\d+)',
f'newrelic/go-agent/v3 v{cur_agent_version}',
line)
replace_line = True
if re.search(r'replace\s+github.com/newrelic/go-agent/v3\s+', line):
continue
dst.write(line)
if replace_line:
my_path = target
back_list = []
while my_path:
my_path, this = os.path.split(my_path)
if this == 'integrations' or this == '.':
break
back_list.append('..')
else:
back_list = None # something went wrong, don't add the line
if back_list:
dst.write('replace github.com/newrelic/go-agent/v3 => ' +
os.path.join(*back_list) + '\n')