install golang
[webi-installers/.git] / golang / golang.bash
1 #!/bin/bash
2
3 # title: Go
4 # homepage: https://golang.org
5 # tagline: The Go Programming Language tools
6 # description: |
7 #   Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
8
9 set -e
10 set -u
11
12 # TODO handle v1.4 / go1.4 / lack of go1.4.0
13 GOLANG_VER=${WEBI_VERSION:-}
14 GOLANG_VER="${GOLANG_VER:-go}" # Search for 'go' at the least
15
16 # WEBI_ARCH uses only slightly different names from GOLANG_ARCH
17 GOLANG_OS="${WEBI_OS}" # linux or darwin
18 GOLANG_ARCH="${WEBI_ARCH}"
19 if [ "x86" == "$GOLANG_ARCH" ]; then
20   GOLANG_ARCH="386"
21 fi
22
23 my_tmp="$WEBI_TMP"
24 sudo_cmd="$WEBI_SUDO"
25
26 #########
27 # BEGIN #
28 #########
29
30 get_golang_version() {
31   # sort -rV    # will sort by version number, but it appears these are already sorted
32   # cut -f 1    # gets only the first column
33   # head -n 1   # gets only the most recent version
34   #  <td class="filename"><a class="download" href="https://dl.google.com/go/go1.13.4.darwin-amd64.tar.gz">go1.13.4.darwin-amd64.tar.gz</a></td>
35   my_char="."
36   my_count=$(awk -F"${my_char}" '{print NF-1}' <<< "${GOLANG_VER}")
37   # get the latest version if partial
38   if [ $my_count -ne 2 ]; then
39     if [ "$GOLANG_VER" != "go" ]; then
40       GOLANG_VER="$GOLANG_VER\\."
41     fi
42     get_http=""
43     if [ -n "$(type -p curl)" ]; then
44       get_http="curl -fsL"
45     elif [ -n "$(type -p wget)" ]; then
46       get_http="wget --quiet -O -"
47     else
48       echo "Found neither 'curl' nor 'wget'. Can't Continue."
49       exit 1
50     fi
51     GOLANG_VER=$($get_http "https://golang.org/dl/" | grep filename.*download | grep ${GOLANG_VER} | grep ${GOLANG_ARCH} | grep ${GOLANG_OS} | cut -d '"' -f 6 | cut -d '/' -f5 | cut -d '.' -f 1-3 | sed 's/\.\(freebsd\|darwin\|linux\|windows\|src\).*//' | head -n 1) \
52         || echo 'error automatically determining current Golang version'
53   fi
54 }
55
56 get_golang_version
57
58 #
59 # golang
60 #
61 golang_install_path=$HOME/.local/opt/${GOLANG_VER}
62 mkdir -p $golang_install_path
63
64 # TODO warn if existing golang in path my take precedence
65 if [ -e "$golang_install_path/bin/go" ]; then
66   # golang of some version is already installed
67   #echo "${GOLANG_VER}" == "$($golang_install_path/bin/go version | cut -d ' ' -f 3 2>/dev/null)"
68   if [ "${GOLANG_VER}" == "$($golang_install_path/bin/go version | cut -d ' ' -f 3 2>/dev/null)" ]; then
69     echo ${GOLANG_VER} already installed at $golang_install_path
70     exit 0
71   fi
72 fi
73
74 GOLANG_PRE="${GOLANG_VER}.${GOLANG_OS}-${GOLANG_ARCH}"
75 GOLANG_REMOTE="https://dl.google.com/go/${GOLANG_PRE}.tar.gz"
76 GOLANG_LOCAL="$my_tmp/${GOLANG_PRE}.tar.gz"
77 GOLANG_UNTAR="$my_tmp/${GOLANG_PRE}"
78
79 if [ -n "$(command -v curl 2>/dev/null | grep curl)" ]; then
80   curl -fSL ${GOLANG_REMOTE} -o ${GOLANG_LOCAL} || echo 'error downloading golang'
81 elif [ -n "$(command -v wget 2>/dev/null | grep wget)" ]; then
82   wget ${GOLANG_REMOTE} -O ${GOLANG_LOCAL} || echo 'error downloading golang'
83 else
84   echo "'wget' and 'curl' are missing. Please run the following command and try again"
85   echo "    sudo apt-get install --yes curl wget"
86   exit 1
87 fi
88
89 mkdir -p ${GOLANG_UNTAR}/
90 # --strip-components isn't portable, switch to portable version by performing move step after untar
91 tar xf ${GOLANG_LOCAL} -C ${GOLANG_UNTAR}/ #--strip-components=1
92 mv ${GOLANG_UNTAR}/go/* ${GOLANG_UNTAR}/
93 rm -rf ${GOLANG_UNTAR}/go # clean up the temporary unzip folder
94 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
95   echo $sudo_cmd rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/"
96   rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/"
97 else
98   echo $sudo_cmd cp -Hr "${GOLANG_UNTAR}/*" "$golang_install_path/"
99   cp -Hr "${GOLANG_UNTAR}"/* "$golang_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${GOLANG_UNTAR}"/* "$golang_install_path/"
100 fi
101 rm -rf "${GOLANG_UNTAR}"
102
103 #######
104 # END #
105 #######
106
107 # TODO add more than one at a time
108 pathman add $golang_install_path/bin
109 mkdir -p $HOME/go/bin
110 pathman add $HOME/go/bin
111 echo "go get golang.org/x/tools/cmd/goimports"
112 $golang_install_path/bin/go get golang.org/x/tools/cmd/goimports > /dev/null 2>/dev/null