-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-go-mod.sh
executable file
·87 lines (72 loc) · 1.38 KB
/
make-go-mod.sh
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
#!/bin/bash
#bazel_output_base=$(bazel info | grep "^output_base: " | awk -F ": " '{print $2}')
#[[ -z $bazel_output_base ]] && {
# echo "Error: no bazel output base from 'bazel info'"
# exit 1
#}
declare -a lines=()
i=0
while IFS= read -r line; do
lines[i]="$line"
let "i++"
done <repositories.bzl
requires=""
replaces=""
in_block=false
for ((i = 0; i < ${#lines[@]}; i++)); do
line=$(echo ${lines[$i]})
if ! $in_block; then
if [ "$line" == "go_repository(" ]; then
in_block=true
name=""
importpath=""
replace=""
version=""
fi
continue
fi
if [ "$line" == ")" ]; then
in_block=false
if [ -z "$name" ] || [ -z "$importpath" ] || [ -z "$version" ]; then
continue
fi
if [ -z "$replace" ]; then
requires="$requires\t$importpath $version\n"
else
replaces="$replaces\t$importpath $version => $replace $version\n"
fi
else
key=$(echo "$line" | awk -F ' = ' '{print $1}')
value=$(echo "$line" | awk -F ' = \"' '{print $2}' | sed 's/",//g')
case "$key" in
"name")
name="$value"
;;
"importpath")
importpath="$value"
;;
"replace")
replace="$value"
;;
"version")
version="$value"
;;
esac
fi
done
req=$(echo -e "$requires")
req=$(echo "$req" | sort)
rep=$(echo -e "$replaces")
rep=$(echo "$rep" | sort)
mirrors=$(cat repositories.txt)
cat <<EOF >go.mod
module github.com
go 1.13
require (
$req
)
replace (
$rep
$mirrors
)
EOF