Last active
March 30, 2022 09:29
-
-
Save mysqto/ea42e5075050e1a188c454fec611c284 to your computer and use it in GitHub Desktop.
script to install latest go on linux
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
#!/usr/bin/env bash | |
function _realpath() { | |
python3 -c "import os; print(os.path.realpath('$1'))" | |
} | |
function installed() { | |
executable="$1" | |
command -v "$executable" >/dev/null 2>&1 | |
} | |
function getgo() { | |
url=$1 | |
prefix=$2 | |
gopkg="$(basename "$url")" | |
[[ -z "$gopkg" ]] && echo "Fatal: invalid url $url" && return 255 | |
[[ -z "$prefix" ]] && prefix=/opt | |
[[ ! -f "/tmp/$gopkg" ]] && echo "downloading $gopkg from $url" && curl -L --progress-bar -o "/tmp/$gopkg" "$url" | |
[[ ! -d "$prefix" ]] && echo "creating directory : $prefix" && mkdir -p "$prefix" | |
cd "$prefix" || return 255 | |
echo "extracting $gopkg to $prefix/go ... " && tar xf "/tmp/$gopkg" | |
cd - >/dev/null 2>&1 || return 255 | |
for file in "$prefix"/go/bin/*; do | |
bin="$(basename "$file")" | |
if installed update-alternatives; then | |
update-alternatives --install /usr/bin/"$bin" "$bin" "$(_realpath "$file")" 1 | |
else | |
rm -rf /usr/bin/"${bin:?}" && ln -s "$(_realpath "$file")" /usr/bin/"${bin:?}" | |
fi | |
done | |
} | |
if ((EUID != 0)); then | |
echo "Granting root privileges for ""$(basename "$0")" | |
if [[ -t 1 ]]; then | |
sudo "$0" "$@" | |
else | |
exec 1>output_file | |
gksu "$0" "$@" | |
fi | |
exit | |
fi | |
os="$(uname)" | |
if [ "$os" != "Linux" ]; then | |
echo "this script is linux only" && exit 255 | |
fi | |
arch="" | |
case $(uname -m) in | |
i386|i686) arch="386" ;; | |
x86_64) arch="amd64" ;; | |
aarch64|arm64) arch="arm64" ;; | |
armv6l) arch="armv6l" ;; | |
esac | |
[[ -z "$arch" ]] && echo "Fatal: can not determine system arch" && exit 255 | |
go_dl_base="https://go.dev/dl/" | |
go_dl_root="https://go.dev" | |
go_url="$(curl -s "$go_dl_base" | grep -Eo "a class=\"download\" href=.*go.*linux-$arch.tar.gz\"" | head -n1 | cut -d'"' -f4)" | |
go_version="$(go version 2>/dev/null | awk '{print $3}')" | |
remote_version="$(basename "$go_url" | sed -e 's/.linux-amd64.tar.gz//')" | |
[[ -n "$go_url" ]] && [[ "$go_version" != "$remote_version" ]] && getgo "$go_dl_root$go_url" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment