Skip to content

Commit a63c4c6

Browse files
authored
Merge pull request #30 from WillAbides/multi-arch
Support more runners
2 parents 3309824 + b428eef commit a63c4c6

File tree

5 files changed

+130
-27
lines changed

5 files changed

+130
-27
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ Look at those outputs. If you want to use GOPATH or GOMODCACHE as input in some
4545
other step, you can just grab it from setup-go-faster\'s output instead of
4646
having to add another step just to set an environment variable.
4747

48+
### Supported Systems
49+
50+
Setup-go-faster supports these runner systems:
51+
52+
| RUNNER_OS | RUNNER_ARCH | go system |
53+
|-----------|-------------|---------------|
54+
| Linux | X86 | linux/386 |
55+
| Linux | X64 | linux/amd64 |
56+
| Linux | ARM64 | linux/arm64 |
57+
| MacOS | X64 | darwin/amd64 |
58+
| MacOS | ARM64 | darwin/arm64 |
59+
| Windows | X86 | windows/386 |
60+
| Windows | X64 | windows/amd64 |
61+
| Windows | ARM64 | windows/arm64 |
62+
4863
### What\'s missing?
4964

5065
Just the `stable` input. I don\'t understand what `stable` adds for

src/lib

+79-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ set -e
44

55
CDPATH="" cd -- "$(dirname -- "$0")/.."
66

7+
supported_system() {
8+
grep -q "'$1'" <<< "
9+
'linux/386'
10+
'linux/amd64'
11+
'linux/arm64'
12+
'darwin/amd64'
13+
'darwin/arm64'
14+
'windows/386'
15+
'windows/amd64'
16+
'windows/arm64'
17+
"
18+
}
19+
720
goos() {
821
case "$RUNNER_OS" in
922
macOS)
@@ -21,6 +34,55 @@ goos() {
2134
esac
2235
}
2336

37+
# get the arch from uname in case RUNNER_ARCH is not set
38+
# copied from https://github.com/client9/shlib
39+
uname_arch() {
40+
arch=$(uname -m)
41+
case $arch in
42+
x86_64) arch="amd64" ;;
43+
x86) arch="386" ;;
44+
i686) arch="386" ;;
45+
i386) arch="386" ;;
46+
aarch64) arch="arm64" ;;
47+
armv5*) arch="armv5" ;;
48+
armv6*) arch="armv6" ;;
49+
armv7*) arch="armv7" ;;
50+
esac
51+
echo ${arch}
52+
}
53+
54+
# Get the arch to download. Use RUNNER_ARCH first, then uname if not set.
55+
goarch() {
56+
case "$RUNNER_ARCH" in
57+
X86)
58+
echo "386"
59+
;;
60+
X64)
61+
echo "amd64"
62+
;;
63+
ARM64)
64+
echo "arm64"
65+
;;
66+
*)
67+
uname_arch
68+
;;
69+
esac
70+
}
71+
72+
go_system() {
73+
echo "$(goos)/$(goarch)"
74+
}
75+
76+
# returns the os part of os/arch
77+
system_os() {
78+
echo "${1%%/*}"
79+
}
80+
81+
# returns the arch part of os/arch
82+
system_arch() {
83+
echo "${1##*/}"
84+
}
85+
2486
debug_out() {
2587
if [ -n "$DEBUG" ]; then
2688
echo "$@" >&2
@@ -39,17 +101,14 @@ sdk_dir() {
39101
echo "$(homedir)/sdk"
40102
}
41103

42-
extension() {
43-
if [ "$(goos)" = "windows" ]; then
44-
echo ".zip"
45-
else
46-
echo ".tar.gz"
47-
fi
48-
}
49-
50104
version_archive_name() {
51105
local version="$1"
52-
echo "$version.$(goos)-amd64$(extension)"
106+
local system="$2"
107+
local extension=".tar.gz"
108+
if [ "$(system_os "$system")" = "windows" ]; then
109+
extension=".zip"
110+
fi
111+
echo "$version.$(system_os "$system")-$(system_arch "$system")$extension"
53112
}
54113

55114
init_tmpdir() {
@@ -65,26 +124,32 @@ init_tmpdir() {
65124

66125
download_go_url() {
67126
local go_version="$1"
68-
archive_name="$(version_archive_name go"$go_version")"
127+
local system="$2"
128+
archive_name="$(version_archive_name go"$go_version" "$system")"
69129
echo "https://storage.googleapis.com/golang/$archive_name"
70130
}
71131

72132
install_go() {
73133
local go_version="${1#go}"
74134
local target_dir="$2"
75135
debug_out "installing go $go_version to $target_dir"
136+
local system
137+
system="$(go_system)"
138+
if ! supported_system "$system"; then
139+
echo "::error ::Unsupported system: $system"
140+
return 1
141+
fi
76142
rm -rf "$target_dir"
77143
mkdir -p "$(dirname "$target_dir")"
78144
tmpdir="$(init_tmpdir)"
79145
cd "$tmpdir"
80146

81-
archive_name="$(version_archive_name go"$go_version")"
147+
archive_name="$(version_archive_name go"$go_version" "$system")"
82148

83149
# 4 retries is 15 seconds of waiting
84-
curl -s --retry 4 --fail -O "$(download_go_url "$go_version")"
150+
curl -s --retry 4 --fail -O "$(download_go_url "$go_version" "$system")"
85151

86-
pwd
87-
if [ "$(extension)" = ".zip" ]; then
152+
if [ "${archive_name: -4}" == ".zip" ]; then
88153
7z x "$archive_name"
89154
else
90155
tar -xzf "$archive_name"

src/lib_test.sh

+34-11
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@ test_homedir() {
1818
}
1919

2020
test_download_go_url() {
21-
assertEquals \
22-
"https://storage.googleapis.com/golang/go1.15.5.linux-amd64.tar.gz" \
23-
"$(RUNNER_OS=Linux download_go_url "1.15.5")"
24-
25-
assertEquals \
26-
"https://storage.googleapis.com/golang/go1.15.5.darwin-amd64.tar.gz" \
27-
"$(RUNNER_OS=macOS download_go_url "1.15.5")"
28-
29-
assertEquals \
30-
"https://storage.googleapis.com/golang/go1.15.5.windows-amd64.zip" \
31-
"$(RUNNER_OS=Windows download_go_url "1.15.5")"
21+
run_test() {
22+
local system="$1"
23+
local go_version="$2"
24+
local want_url="$3"
25+
got_url="$(download_go_url "$go_version" "$system")"
26+
assertEquals "$want_url" "$got_url"
27+
}
28+
29+
run_test linux/amd64 1.15.5 "https://storage.googleapis.com/golang/go1.15.5.linux-amd64.tar.gz"
30+
run_test linux/386 1.15.5 "https://storage.googleapis.com/golang/go1.15.5.linux-386.tar.gz"
31+
run_test linux/arm64 1.15.5 "https://storage.googleapis.com/golang/go1.15.5.linux-arm64.tar.gz"
32+
run_test darwin/amd64 1.15.5 "https://storage.googleapis.com/golang/go1.15.5.darwin-amd64.tar.gz"
33+
run_test darwin/arm64 1.15.5 "https://storage.googleapis.com/golang/go1.15.5.darwin-arm64.tar.gz"
34+
run_test windows/amd64 1.15.5 "https://storage.googleapis.com/golang/go1.15.5.windows-amd64.zip"
35+
run_test windows/386 1.15.5 "https://storage.googleapis.com/golang/go1.15.5.windows-386.zip"
36+
run_test windows/arm64 1.15.5 "https://storage.googleapis.com/golang/go1.15.5.windows-arm64.zip"
3237
}
3338

3439
test_is_precise_version() {
@@ -143,4 +148,22 @@ x;go1.15.7'
143148
done
144149
}
145150

151+
test_supported_system() {
152+
assertTrue "linux/amd64" 'supported_system "linux/amd64"'
153+
assertTrue "linux/386" 'supported_system "linux/386"'
154+
assertTrue "linux/arm64" 'supported_system "linux/arm64"'
155+
assertTrue "darwin/amd64" 'supported_system "darwin/amd64"'
156+
assertTrue "darwin/arm64" 'supported_system "darwin/arm64"'
157+
assertTrue "windows/amd64" 'supported_system "windows/amd64"'
158+
assertTrue "windows/386" 'supported_system "windows/386"'
159+
assertTrue "windows/arm64" 'supported_system "windows/arm64"'
160+
161+
assertFalse "linux/arm" 'supported_system "linux/arm"'
162+
assertFalse "darwin/386" 'supported_system "darwin/386"'
163+
assertFalse "asdf" 'supported_system "asdf"'
164+
assertFalse "" 'supported_system ""'
165+
assertFalse " " 'supported_system " "'
166+
167+
}
168+
146169
. ./external/shunit2

src/lib_test_long.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test_install_go() {
1717
version="1.15.4"
1818
install_go "$version" "$target"
1919
got_version="$("$target/bin/go" version)"
20-
assertEquals "go version go1.15.4 $(goos)/amd64" "$got_version"
20+
assertEquals "go version go1.15.4 $(go_system)" "$got_version"
2121
)
2222
}
2323

src/semver-select

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ download() {
2222
skip_download && return
2323
mkdir -p "$(dirname "$bin_path")"
2424
url="https://github.com/WillAbides/semver-select/releases/download/v"
25-
url+="$target_version/semver-select_${target_version}_$(goos)_amd64"
25+
url+="$target_version/semver-select_${target_version}_$(goos)_$(goarch)"
2626
[ "$(goos)" = "windows" ] && url+=".exe"
2727
curl -s --fail -o "$bin_path" --retry 4 -L "$url"
2828
chmod +x "$bin_path"

0 commit comments

Comments
 (0)