Last active
April 23, 2024 09:01
-
-
Save LaurenceJJones/aacedfd4438a811780951b2c40431e3a to your computer and use it in GitHub Desktop.
Script to automatically download golang version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
VERSION=${VERSION:-1.22.2} | |
# Detect OS platform amd64 | |
OS=$(uname -s | tr '[:upper:]' '[:lower:]') | |
ARCH=$(uname -m | tr '[:upper:]' '[:lower:]') | |
if [ "$ARCH" = "x86_64" ]; then | |
ARCH="amd64" | |
fi | |
# Detect OS platform arm64 | |
if [ "$ARCH" = "aarch64" ]; then | |
ARCH="arm64" | |
fi | |
## Utilities ## | |
download() { | |
if [ -z "$1" ]; then | |
echo "download() requires a URL as first argument" | |
exit 1 | |
fi | |
if [ -z "$2" ]; then | |
echo "download() requires a destination directory as second argument" | |
exit 1 | |
fi | |
if [ ! -d "$2" ]; then | |
echo "$2 is not a directory" | |
exit 1 | |
fi | |
if command -v curl >/dev/null; then | |
cd "$2" || (echo "Could not cd to $2" && exit 1) | |
# older versions of curl don't support --output-dir | |
curl -sSLO --fail --remote-name "$1" | |
cd - >/dev/null || exit | |
elif command -v wget >/dev/null; then | |
wget -nv -P "$2" "$1" | |
else | |
echo "Neither curl nor wget is available, cannot download files." | |
exit 1 | |
fi | |
} | |
# Download Go | |
download "https://go.dev/dl/go${VERSION}.${OS}-${ARCH}.tar.gz" "/tmp" | |
# Exract Go | |
sudo tar -C /usr/local -xzf "/tmp/go${VERSION}.${OS}-${ARCH}.tar.gz" | |
# Add Go to PATH | |
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc | |
# Reload bashrc | |
echo "Plase reload your bashrc: source ~/.bashrc" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment