-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo_install
More file actions
executable file
·167 lines (151 loc) · 3.32 KB
/
go_install
File metadata and controls
executable file
·167 lines (151 loc) · 3.32 KB
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
#!/bin/sh
#
# Copyright 2026 Samvel Khalatyan. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
set -x
set -e
# keep-sorted start
readonly CACHE_PATH=${HOME}/.cache/go_install
readonly GO_ROOT_PREFIX=/usr/local
readonly GO_VERSION=${GO_VERSION:-1.26.0}
readonly LOG_PATH=go_install.log # relative to TMPDIR
# keep-sorted end
CHECKSUM_FILE=$(realpath go_checksum_sha256) # relative to script
readonly CHECKSUM_FILE
err() {
echo "$@" >&2
}
check_platform() {
case $(uname -s) in
*Linux*) ;;
*)
return 1
;;
esac
case $(uname -v) in
*FreeBSD*) ;;
*)
return 1
;;
esac
}
check_privileges() {
if [ $(id -u) = "0" ]; then
return
fi
case $(id -nG) in
*sudoer*)
return
;;
esac
return 1
}
is_go_installed() {
local go_version=
if ! go_version=$(go env GOVERSION 2>/dev/null); then
return 1
fi
if [ "$go_version" != "go${GO_VERSION}" ]; then
return 1
fi
return
}
make_url() {
local os=${1:?'make_url: missing os'}
echo "https://go.dev/dl/go${GO_VERSION}.${os}-amd64.tar.gz"
}
download() {
local os=${1:?'install: missing os'}
local url
url=$(make_url ${os})
local archive
archive=$(basename ${url})
local archive_in_cache
archive_in_cache="${CACHE_PATH}/${archive}"
if test -e "${archive_in_cache}"; then
echo ".. download ${url} (cached)"
return
fi
echo ".. download ${url}"
{
wget --quiet --show-progress ${url}
sha256sum -c ${CHECKSUM_FILE} --ignore-missing
mv -nv ${archive} ${archive_in_cache}
} >>${LOG_PATH}
}
extract() {
local os=${1:?'extract: missing os'}
local url
url=$(make_url ${os})
local archive
archive=$(basename ${url})
local archive_in_cache
archive_in_cache="${CACHE_PATH}/${archive}"
echo ".. extract ${archive}"
{
tar -xzf "${archive_in_cache}"
} >>${LOG_PATH}
}
patch_from() {
local os=${1:?'patch_from: missing os'}
local url
url=$(make_url ${os})
local archive
archive=$(basename ${url})
local archive_in_cache
archive_in_cache="${CACHE_PATH}/${archive}"
local link_freebsd_path=go/pkg/tool/freebsd_amd64/link
local link_linux_path=go/pkg/tool/linux_amd64/link
echo ".. patch ${link_linux_path}"
{
tar -xzf ${archive_in_cache} ${link_freebsd_path}
} >>${LOG_PATH}
mv ${link_freebsd_path} ${link_linux_path}
rm -rf ${link_freebsd_path%/*}
}
make_goroot() {
local version=${GO_VERSION%.*} # MAJOR.MINOR
local suffix=$(echo ${version} | tr -d '.') # {MAJOR}{MINOR}
echo ${GO_ROOT_PREFIX}/go${suffix}
}
install() {
local goroot
goroot=$(make_goroot)
echo ".. install ${goroot} [need sudo]"
if test -d ${goroot}; then
sudo rm -rf ${goroot}
fi
sudo mv -i ${PWD}/go ${goroot}
}
run() {
local tmpdir
tmpdir=$(mktemp -d)
trap "rm -rf ${tmpdir}" EXIT
cd ${tmpdir}
if ! test -d "${CACHE_PATH}"; then
mkdir ${CACHE_PATH}
fi
download freebsd
download linux
extract linux
patch_from freebsd
install
}
main() {
if ! check_platform; then
err "unsupported platform - want Linux compatibility under FreeBSD"
return 1
fi
if ! check_privileges; then
err "the effective user does not have sudo privileges"
return 1
fi
if is_go_installed; then
echo "Go is installed to $(go env GOROOT)"
return 0
fi
run
}
main