-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib
executable file
·176 lines (148 loc) · 3.51 KB
/
lib
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
#!/bin/bash
set -e
CDPATH="" cd -- "$(dirname -- "$(dirname -- "$0")")"
goos() {
case "$RUNNER_OS" in
macOS)
echo "darwin"
;;
Linux)
echo "linux"
;;
Windows)
echo "windows"
;;
*)
uname -s | tr '[:upper:]' '[:lower:]'
;;
esac
}
debug_out() {
if [ -n "$DEBUG" ]; then
echo "$@" >&2
fi
}
homedir() {
if [ "$(goos)" = "windows" ]; then
echo "$USERPROFILE"
else
echo "$HOME"
fi
}
sdk_dir() {
echo "$(homedir)/sdk"
}
extension() {
if [ "$(goos)" = "windows" ]; then
echo ".zip"
else
echo ".tar.gz"
fi
}
version_archive_name() {
local version="$1"
echo "$version.$(goos)-amd64$(extension)"
}
init_tmpdir() {
local tmpdir="$RUNNER_WORKSPACE"
tmpdir="${tmpdir:-"$TMPDIR"}"
tmpdir="$tmpdir/setup-go-faster/tmp"
mkdir -p "$tmpdir"
rm -rf "$tmpdir"
mkdir -p "$tmpdir"
trap 'rm -rf "$tmpdir"' EXIT
echo "$tmpdir"
}
download_go_url() {
local go_version="$1"
archive_name="$(version_archive_name go"$go_version")"
echo "https://storage.googleapis.com/golang/$archive_name"
}
install_go() {
local go_version="$1"
local target_dir="$2"
debug_out "installing go $go_version to $target_dir"
rm -rf "$target_dir"
mkdir -p "$(dirname "$target_dir")"
tmpdir="$(init_tmpdir)"
cd "$tmpdir"
archive_name="$(version_archive_name go"$go_version")"
# 4 retries is 15 seconds of waiting
curl -s --retry 4 --fail -O "$(download_go_url "$go_version")"
pwd
if [ "$(extension)" = ".zip" ]; then
7z x "$archive_name"
else
tar -xzf "$archive_name"
fi
mv "go" "$target_dir"
cd - >/dev/null
}
add_to_system_path() {
element="$1"
if [ "$(goos)" == "windows" ]; then
element="$(echo "/$element" | sed -e 's|\\|/|g' | sed 's/://')"
fi
echo "$element:$PATH"
}
add_to_github_path() {
element="$1"
echo "$element" >>"$GITHUB_PATH"
}
exe_name() {
suffix=""
[ "$(goos)" = "windows" ] && suffix=".exe"
echo "$1$suffix"
}
is_precise_version() {
[[ $1 =~ ^[0-9]+(.([0-9]+)(.[0-9]+)?)?([A-Za-z0-9]+)?$ ]] || return 1
# false when minor is >= 21 and patch is empty
[ -z "${BASH_REMATCH[2]}" ] || [ "${BASH_REMATCH[2]}" -lt 21 ] || [ -n "${BASH_REMATCH[3]}" ]
}
select_go_version() {
local constraint="$1"
local versions="$2"
local got
got="$(echo "$versions" | ./src/select_go_version.pl "$constraint")" || return
echo "$got"
}
select_local_version() {
local constraint="$1"
local go_tool_cache="$2"
local local_versions
local_versions="$(ls "$go_tool_cache")"
# if this is a plain version instead of a constraint, only an exact match will work
if is_precise_version "$constraint"; then
for v in $local_versions; do
if [ "$v" = "$constraint" ]; then
echo "$v" && return
fi
done
return
fi
# handle the 1.x type constraint
if ver="$(select_go_version "$constraint" "$local_versions")"; then
if [ -n "$ver" ]; then
echo "$ver" && return
fi
fi
# handle other constraints
local_matches="$(echo "$local_versions" | ./src/semver-select -c "$constraint" -n 1 -i -)"
if [ -n "$local_matches" ]; then
echo "$local_matches" | tail -1
fi
}
select_remote_version() {
local constraint="$1"
local versions="$2"
# shellcheck disable=SC2001
versions="$(echo "$versions"| sed 's/^go//g')"
if got="$(select_go_version "$constraint" "$versions")"; then
echo "$got" && return
fi
# don't try semver-select on a precise version
if is_precise_version "$constraint"; then
return
fi
echo "$versions" | ./src/semver-select -c "$constraint" -n 1 -i -
}