Skip to content

Instantly share code, notes, and snippets.

@douglarek
Forked from aofei/switchgo.sh
Last active February 18, 2021 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglarek/e91668cd1675f67ff92ee13b9106946d to your computer and use it in GitHub Desktop.
Save douglarek/e91668cd1675f67ff92ee13b9106946d to your computer and use it in GitHub Desktop.
A fish shell script for switching the version of Go.
function go_switch --argument-names 'verzion' # since version in fish shell is a bulit-in var
if not test -n "$verzion"
echo "Usage: go_switch [version]"
return 1
end
if not type -q go # check bootstrap go
if [ (uname -i) != "x86_64" ]
echo "only x86_64 supported"; and return 1
end
set os "linux"
switch (uname)
case Linux
case Darwin
set os "darwin"
case "*"
echo "only darwin and linux supported"; and return 1
end
rm -rf ~/sdk/go$verzion; and mkdir -p ~/sdk/go$verzion; and \
curl "https://storage.googleapis.com/golang/go$verzion.$os-amd64.tar.gz" -o ~/sdk/go$verzion.tar.gz; and \
tar -xvf ~/sdk/go$verzion.tar.gz; and \
mv ~/sdk/go ~/sdk/go$verzion; and rm -f ~/sdk/go$verzion.tar.gz; or return 1
ln -sf ~/sdk/go$verzion/bin/go ~/bin/go; or return 1
else
if not type -q go{$verzion}
echo "Go $verzion doesn't exist, start downloading..."
go get golang.org/dl/go{$verzion}; or return 1
go{$verzion} download; or return 1
end
go{$verzion} download &> /dev/null; or return 1 # maybe go binary not downloaded, so try it
set go_bin (command -v "go$verzion")
ln -sf $go_bin ~/bin/go; or return 1
end
echo "Switched to Go $verzion"
set -gx fish_greeting ''
exec fish # reload fish
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment