Last active
April 3, 2023 01:38
-
-
Save Burugux/df0ac9e91145ade070e1680ab79c2103 to your computer and use it in GitHub Desktop.
Update Go versions
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 | |
# This is just a little script that i use to upgrade the version of Go on | |
# my computer. | |
set -euf -o pipefail | |
# pattern used to grab download links from https://golang.org/dl/ | |
GREP_PATTERN="https:\/\/dl\.google\.com\/go\/go([0-9\.]+)\.linux-amd64\.tar\.gz" | |
# checks if a command is installed | |
check_cmd(){ | |
command -v "$1" > /dev/null 2>&1; | |
} | |
# check if a certain command is available.If not it terminates. | |
need_cmd() { | |
if ! check_cmd "$1"; then | |
err "need '$1' (command not found)" | |
fi | |
} | |
# terminates the script | |
err(){ | |
say "$1" >&2 | |
exit 1 | |
} | |
say(){ | |
printf 'update_go: %s\n' "$1" | |
} | |
setup(){ | |
# remove the old version | |
old_version="$(go version)" | |
echo "+ Removing previously installed ${old_version}" | |
sudo rm -rf /usr/local/go | |
# install new version | |
echo "+ Installing $1" | |
sudo tar -C /usr/local -xzf "$HOME"/"$1".linux-amd64.tar.gz | |
# run test hello world | |
test_program "$1" | |
} | |
# creates a sample hello world and runs it to test the new installation | |
function test_program(){ | |
mkdir -p "$GOPATH"/src/test | |
TEST_DIR_PATH="$GOPATH/src/test/main.go" | |
cat > "$TEST_DIR_PATH" <<- EOM | |
package main | |
import "fmt" | |
func main() { | |
fmt.Printf("Output=>>>> Hello, world from $1\n") | |
} | |
EOM | |
echo "+ Running test hello world..." | |
go run "$TEST_DIR_PATH" | |
# delete downloaded update from the home directory | |
rm -rf "$HOME"/"${1}".linux-amd64.tar.gz | |
echo "All done. You are set to Go :)" | |
} | |
# download the update with either wget or curl | |
downloader(){ | |
need_cmd grep | |
need_cmd cut | |
need_cmd head | |
local dl_tool | |
if check_cmd wget; then | |
dl_tool=wget | |
elif check_cmd curl;then | |
dl_tool=curl | |
else | |
dl_tool="Neither wget nor curl found" | |
fi | |
if [ "$dl_tool" == wget ]; then | |
echo "+ Finding latest version of Go for linux/AMD64..." | |
url="$(wget -qO- https://golang.org/dl/ | grep -oP "$GREP_PATTERN" | head -n 1 )" | |
latest_version="$(echo "$url" | grep -oP 'go[0-9\.]+' | cut -c 1-8)" | |
echo "+ Found ${latest_version} for linux/AMD64. Downloading..." | |
wget --quiet --continue --show-progress -P "$HOME" "${url}" | |
setup "${latest_version}" | |
elif [ "$dl_tool" == curl ]; then | |
echo "+ Finding latest version of Go for linux/AMD64..." | |
url="$(curl --silent https://golang.org/dl/ | grep -oP "$GREP_PATTERN" | head -n 1 )" | |
latest_version="$(echo "$url" | grep -oP 'go[0-9\.]+' | cut -c 1-8)" | |
echo "+ Found ${latest_version} for linux/AMD64. Downloading..." | |
curl --show-error --fail --location "${url}" > "$HOME"/"$latest_version".linux-amd64.tar.gz | |
setup "${latest_version}" | |
else | |
err "${dl_tool}" | |
fi | |
} | |
downloader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment