-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-go.sh
More file actions
executable file
·69 lines (56 loc) · 2.22 KB
/
update-go.sh
File metadata and controls
executable file
·69 lines (56 loc) · 2.22 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
#!/usr/bin/env bash
# update-go.sh - script for automating Go update on my Debian machine
set -eu
# Constants for operations
# Make sure these don't end with a trailing slash
DOWNLOADS_DOMAIN="https://go.dev/dl"
DOWNLOAD_DIRECTORY="$HOME/downloads"
GO_INSTALL_PARENT="/usr/local"
INSTALLED_VERSION=$(go version 2>/dev/null | cut -d' ' -f3 || echo "not installed")
# Avoid destructive rm -rf later down the line
if [ -z "$GO_INSTALL_PARENT" ]; then
echo "\$GO_INSTALL_PARENT is empty, this could be destructive - aborting!"
exit 1
fi
# Detect system architecture
case "$(uname -m)" in
x86_64) go_arch="amd64" ;;
aarch64) go_arch="arm64" ;;
i686) go_arch="386" ;;
*)
echo "Unsupported architecture: $(uname -m)"
exit 1
;;
esac
# Fetch data about available downloads
DOWNLOAD_DATA=$(curl -s "$DOWNLOADS_DOMAIN/?mode=json" | jq -c --arg go_arch $go_arch '.[0].files[] | select(.os == "linux" and .arch == $go_arch)')
if [ -z "$DOWNLOAD_DATA" ]; then
echo "Error: data about available downloads is empty"
exit 1
fi
#echo ${DOWNLOAD_DATA} > file_data.json # debug
data_filename="$(jq -r .filename <<< $DOWNLOAD_DATA)"
data_version="$(jq -r .version <<< $DOWNLOAD_DATA)"
data_hash="$(jq -r .sha256 <<< $DOWNLOAD_DATA)"
# Show version information and ask user if we should proceed
printf "Version installed: $INSTALLED_VERSION\nVersion available: $data_version\n\n"
read -rp "Continue with installing into \"$GO_INSTALL_PARENT\" [y/N]: " CONFIRM
if [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; then
echo "Aborting."
exit 0
fi
# Download the golang archive
echo "Downloading $DOWNLOADS_DOMAIN/$data_filename"
curl -sL --create-dirs -O --output-dir "$DOWNLOAD_DIRECTORY" "$DOWNLOADS_DOMAIN/$data_filename"
file_location="$DOWNLOAD_DIRECTORY/$data_filename"
# Verify whether file was downloaded successfully
echo "$data_hash $file_location" | sha256sum --check --status || {
echo "Error: downloaded file didn't match sha256 hash online: $data_hash"
exit 1
}
# Unpack the downloaded archive into $GO_INSTALL_PARENT/go
echo "Removing old installation data at $GO_INSTALL_PARENT/go"
sudo rm -rf "$GO_INSTALL_PARENT/go"
echo "Unpacking new golang archive into $GO_INSTALL_PARENT"
sudo tar -C "$GO_INSTALL_PARENT" -xzf "$file_location"
echo "Finished."