improve go script (#2670)

This commit is contained in:
nyxnor
2021-11-30 22:54:42 +00:00
committed by GitHub
parent 38749562c4
commit 792b0a7bb2

View File

@@ -1,102 +1,60 @@
#!/bin/bash #!/usr/bin/env sh
# command info # set version, check: https://golang.org/dl/
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then goVersion="1.17.3"
echo "config script to install Go" downloadFolder="/home/admin/download"
echo "bonus.go.sh [on|off]"
exit 1
fi
# switch on usage(){
if [ "$1" = "1" ] || [ "$1" = "on" ]; then printf "Config script to install or remove Go\n"
printf "./bonus.go.sh [on|off]\n"
exit 1
}
# set version, check: https://github.com/golang/go/releases case "$1" in
goVersion="1.16.2"
# get cpu architecture 1|on) # switch on
isARM=$(uname -m | grep -c 'arm') . /etc/profile # get Go vars - needed if there was no log-out since Go installed
isAARCH64=$(uname -m | grep -c 'aarch64') printf "Check Framework: Go\n"
isX86_64=$(uname -m | grep -c 'x86_64') if go version 2>/dev/null | grep -q "go" ; then
printf "\nVersion of Go requested already installed.\n"
# make sure go is installed go version
printf "\n"
# get Go vars - needed if there was no log-out since Go installed else
source /etc/profile architecture="$(uname -m)"
case "${architecture}" in
echo "Check Framework: Go" arm*) goOSversion="armv6l";;
goInstalled=$(go version 2>/dev/null | grep -c 'go') aarch64) goOSversion="arm64";;
if [ ${goInstalled} -eq 0 ];then x86_64) goOSversion="amd64";;
if [ ${isARM} -eq 1 ] ; then *) printf %s"Not available for architecture=${architecture}\n"; exit 1
goOSversion="armv6l" esac
fi printf %s"\n*** Installing Go v${goVersion} for ${goOSversion} \n***"
if [ ${isAARCH64} -eq 1 ] ; then wget https://dl.google.com/go/go${goVersion}.linux-${goOSversion}.tar.gz -P ${downloadFolder}
goOSversion="arm64" if [ ! -f "${downloadFolder}/go${goVersion}.linux-${goOSversion}.tar.gz" ]; then
fi printf "!!! FAIL !!! Download failed.\n"
if [ ${isX86_64} -eq 1 ] ; then rm -fv go${goVersion}.linux-${goOSversion}.tar.gz*
goOSversion="amd64"
fi
echo "*** Installing Go v${goVersion} for ${goOSversion} ***"
# wget https://storage.googleapis.com/golang/go${goVersion}.linux-${goOSversion}.tar.gz
cd /home/admin/download
wget https://dl.google.com/go/go${goVersion}.linux-${goOSversion}.tar.gz
if [ ! -f "./go${goVersion}.linux-${goOSversion}.tar.gz" ]
then
echo "!!! FAIL !!! Download not success."
rm -f go${goVersion}.linux-${goOSversion}.tar.gz*
exit 1 exit 1
fi
sudo tar -C /usr/local -xzf ${downloadFolder}/go${goVersion}.linux-${goOSversion}.tar.gz
rm -fv ${downloadFolder}/go${goVersion}.linux-${goOSversion}.tar.gz*
sudo mkdir -v /usr/local/gocode
sudo chmod -v 777 /usr/local/gocode
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/usr/local/gocode
export PATH=$PATH:$GOPATH/bin
sudo grep -q "GOROOT=" /etc/profile || { printf "\nGOROOT=/usr/local/go\nPATH=\$PATH:\$GOROOT/bin/\nGOPATH=/usr/local/gocode\nPATH=\$PATH:\$GOPATH/bin/\n\n" | sudo tee -a /etc/profile; }
go env -w GOPATH=/usr/local/gocode # set GOPATH https://github.com/golang/go/wiki/SettingGOPATH
go version | grep -q "go" || { printf "FAIL: Unable to install Go\n"; exit 1; }
printf %s"Installed $(go version 2>/dev/null)\n\n"
fi fi
sudo tar -C /usr/local -xzf go${goVersion}.linux-${goOSversion}.tar.gz ;;
rm -f go${goVersion}.linux-${goOSversion}.tar.gz*
sudo mkdir /usr/local/gocode
sudo chmod 777 /usr/local/gocode
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/usr/local/gocode
export PATH=$PATH:$GOPATH/bin
if [ $(cat /etc/profile | grep -c "GOROOT=") -eq 0 ]; then
sudo bash -c "echo 'GOROOT=/usr/local/go' >> /etc/profile"
sudo bash -c "echo 'PATH=\$PATH:\$GOROOT/bin/' >> /etc/profile"
sudo bash -c "echo 'GOPATH=/usr/local/gocode' >> /etc/profile"
sudo bash -c "echo 'PATH=\$PATH:\$GOPATH/bin/' >> /etc/profile"
fi
# set GOPATH https://github.com/golang/go/wiki/SettingGOPATH 0|off) # switch off
go env -w GOPATH=/usr/local/gocode printf "*** REMOVING GO ***\n"
sudo rm -rf /usr/local/go /usr/local/gocode
goInstalled=$(go version 2>/dev/null | grep -c 'go') printf "OK Go removed.\n"
fi ;;
if [ ${goInstalled} -eq 0 ];then
echo "FAIL: Was not able to install Go"
sleep 4
exit 1
fi
correctGoVersion=$(go version | grep -c "go${goVersion}") *) usage
if [ ${correctGoVersion} -eq 0 ]; then
echo "WARNING: You work with an untested version of GO - should be ${goVersion} .. trying to continue"
go version
sleep 3
echo ""
fi
# setting value in raspi blitz config esac
echo ""
echo "Installed $(go version)"
echo ""
exit 0
fi
# switch off
if [ "$1" = "0" ] || [ "$1" = "off" ]; then
# setting value in raspiblitz config
echo "*** REMOVING GO ***"
sudo rm -rf /usr/local/go
sudo rm -rf /usr/local/gocode
echo "OK Go removed."
exit 0
fi
echo "FAIL - Unknown Parameter $1"
exit 1