Skip to content

Instantly share code, notes, and snippets.

@blademainer
Last active January 27, 2021 08:04
Show Gist options
  • Save blademainer/854f994c26198774916d438da8c35201 to your computer and use it in GitHub Desktop.
Save blademainer/854f994c26198774916d438da8c35201 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# set -x
version="$1"
INSTALL_DIR="/usr/local"
os="`uname -s`"
arc="`uname -m`"
curl "https://golang.org/dl/" > dl.html
[ -z "`cat dl.html`" ] && exit -1
#cat dl.html | grep -i "$os" | grep "64" | grep -i ".tar.gz" | grep -o "href=\".*\"" | grep -oE "https://(\w|\.|\/|-)+" > result.tmp
cat dl.html | grep -i "$os" | grep "64" | grep -i ".tar.gz" | grep -o "href=\".*\"" | grep -oE "\".*\"" | grep -oE "[^\"]+" > result.tmp
[ -z "`cat result.tmp`" ] && echo "not found pkg!" && exit -1
rm -f dl.html
url="`cat result.tmp | head -n 1`"
if [ "____${version}" != "____" ]; then
url="`cat result | grep $version`"
fi
if [[ "$url" == /* ]]; then
url="https://golang.org$url"
fi
echo "url====$url"
v=`echo "$url" | grep -Eo "go(\d|\.)+\d+"`
echo "version===$v"
TARGET=$INSTALL_DIR/$v
echo "target===$TARGET"
sudo mkdir -p ${TARGET}
sudo chmod -R 777 ${TARGET}
curl -L "$url" > /tmp/go.tar.gz
sudo tar -zxvf /tmp/go.tar.gz -C "${TARGET}"
sudo rm -f /usr/local/go
sudo ln -s ${TARGET}/go /usr/local/go
if [ -n "`cat /etc/profile | grep -E 'export\s+GOROOT='`" ]; then
cat /etc/profile | sed "s/export *GOROOT=.*//g" > /tmp/profile
sudo mv /etc/profile /etc/profile.`date '+%s'`
sudo mv /tmp/profile /etc/profile
fi
echo "echo 'export GOROOT=/usr/local/go' >> /etc/profile" | sudo sh
source /etc/profile
rm -f /tmp/go.tar.gz
rm -f result.tmp
@blademainer
Copy link
Author

blademainer commented Jan 4, 2021

USAGE

curl "https://gist.githubusercontent.com/blademainer/854f994c26198774916d438da8c35201/raw/f30f4529a75af87f593d47610e4e901234a54f5d/install_go" | sudo sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment