forked from terraform-ibm-modules/common-dev-assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraformDocGoMod.py
executable file
·59 lines (47 loc) · 1.67 KB
/
terraformDocGoMod.py
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
#!/usr/bin/python
import re
import sys
from pathlib import Path
from subprocess import PIPE, Popen
from urllib.parse import urlparse
# Set go.mod file with the correct module repo
def set_go_mod(path, module_url):
with open(path, "r") as file:
lines = file.readlines()
if len(lines) > 0:
expected_line = "module " + module_url
replace_module = False
for index, line in enumerate(lines):
regex = re.search(r"module.*?github.*?", line)
if regex:
regex_result = regex.string.strip()
if regex_result.lower() != expected_line.lower():
replace_module = True
break
if replace_module:
lines[index] = expected_line + "\n"
with open(path, "w") as writer:
writer.writelines(lines)
# get repository url
def get_module_url():
get_repository_url_command = "git config --get remote.origin.url"
proc = Popen(get_repository_url_command, stdout=PIPE, stderr=PIPE, shell=True)
output, error = proc.communicate()
full_url = output.decode("utf-8").strip()
if proc.returncode != 0:
print(error)
sys.exit(proc.returncode)
# urlparse can not be used for git urls
if full_url.startswith("http"):
output = urlparse(full_url)
module_url = output.hostname + output.path
else:
module_url = full_url.replace("git@", "").replace(":", "/")
return module_url.replace(".git", "")
def main():
go_mod_path = Path("tests/go.mod")
if go_mod_path.is_file():
module_url = get_module_url()
if module_url:
set_go_mod(go_mod_path, module_url)
main()