Skip to content

Instantly share code, notes, and snippets.

@jwhett
Last active December 14, 2021 23:53
Show Gist options
  • Save jwhett/9843c3368e46eb3f075473c881e93188 to your computer and use it in GitHub Desktop.
Save jwhett/9843c3368e46eb3f075473c881e93188 to your computer and use it in GitHub Desktop.
updating go from the terminal
#!/usr/bin/fish
set -l deps fzf rg curl sort tar
set -l destination ~/.local
set -l go_loc $destination/go
set -l version_home https://go.dev/dl/
set -l dl_base https://dl.google.com/go
set -l ERR_MISSING_DEP 1
set -l ERR_INSTALL 2
# Check for deps
for dep in $deps
if ! type $dep -p 1>/dev/null
echo "❌Missing dependency: $dep" >&2
exit $ERR_MISSING_DEP
end
end
# Setup header info
if ! type go -p 1>/dev/null
set header "Go Install - Select a version to install."
else
set UPDATE 0
set header "Go Update - Current version: " (go version)
end
# Select a version of Go to download
set -l target_version (curl -s "$version_home" | \
rg -o -e "go\d\.\d+\.\d+\.linux-amd64.tar.gz" | \
sort -u | \
fzf \
+m \
--tac \
--layout=reverse \
--header "$header")
if test -z "$target_version"
echo "⚠️ Selection aborted"
# This is not considered an error
exit
end
# If we're updating an installed version, remove
# the previous version
if set -q UPDATE
rm -r "$go_loc"
end
# Download selected version
echo "⌛ Installing $target_version"
curl -s "$downloads/$target_version" "$dl_base/$target_version" | tar -C "$destination" -zx
if test $status -ne 0
echo "❌ Failed to install that version" >&2
exit $ERR_INSTALL
end
if test -d "$go_loc"
echo "👍 Updated to" (go version)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment