show more output
[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 # examples: |
9 #   ```bash
10 #   mkdir -p hello/
11 #   pushd hello/
12 #   ```
13 #   <br/>   
14 #
15 #   ```bash
16 #   cat << EOF >> main.go
17 #   package main
18 #   
19 #   import (
20 #     "fmt"
21 #   )
22 #   
23 #   func main () {
24 #     fmt.Println("Hello, World!")
25 #   }
26 #   EOF
27 #   ```
28 #   <br/>   
29 #
30 #   ```bash
31 #   go fmt ./...
32 #   go build .
33 #   ./hello
34 #   > Hello, World!
35 #   ```
36
37 set -e
38 set -u
39
40 # TODO handle v1.4 / go1.4 / lack of go1.4.0
41 GOLANG_VER=${X_WEBI_VERSION:-}
42 GOLANG_VER="${GOLANG_VER:-go}" # Search for 'go' at the least
43
44 # WEBI_ARCH uses only slightly different names from GOLANG_ARCH
45 GOLANG_OS="${WEBI_OS}" # linux or darwin
46 GOLANG_ARCH="${WEBI_ARCH}"
47 if [ "x86" == "$GOLANG_ARCH" ]; then
48   GOLANG_ARCH="386"
49 fi
50
51 my_tmp="$WEBI_TMP"
52 sudo_cmd="$WEBI_SUDO"
53
54 #########
55 # BEGIN #
56 #########
57
58 get_golang_version() {
59   # sort -rV    # will sort by version number, but it appears these are already sorted
60   # cut -f 1    # gets only the first column
61   # head -n 1   # gets only the most recent version
62   #  <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>
63   my_char="."
64   my_count=$(awk -F"${my_char}" '{print NF-1}' <<< "${GOLANG_VER}")
65   # get the latest version if partial
66   if [ $my_count -ne 2 ]; then
67     if [ "$GOLANG_VER" != "go" ]; then
68       GOLANG_VER="$GOLANG_VER\\."
69     fi
70     get_http=""
71     if [ -n "$(type -p curl)" ]; then
72       get_http="curl -fsL"
73     elif [ -n "$(type -p wget)" ]; then
74       get_http="wget --quiet -O -"
75     else
76       echo "Found neither 'curl' nor 'wget'. Can't Continue."
77       exit 1
78     fi
79     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) \
80         || echo 'error automatically determining current Golang version'
81   fi
82 }
83
84 echo -n "Checking for latest golang version... "
85 get_golang_version
86 echo $GOLANG_VER
87
88 #
89 # golang
90 #
91 golang_install_path=$HOME/.local/opt/${GOLANG_VER}
92 mkdir -p $golang_install_path
93
94 # TODO warn if existing golang in path my take precedence
95 if [ -e "$golang_install_path/bin/go" ]; then
96   # golang of some version is already installed
97   #echo "${GOLANG_VER}" == "$($golang_install_path/bin/go version | cut -d ' ' -f 3 2>/dev/null)"
98   if [ "${GOLANG_VER}" == "$($golang_install_path/bin/go version | cut -d ' ' -f 3 2>/dev/null)" ]; then
99     echo ${GOLANG_VER} already installed at $golang_install_path
100     exit 0
101   fi
102 fi
103
104 GOLANG_PRE="${GOLANG_VER}.${GOLANG_OS}-${GOLANG_ARCH}"
105 GOLANG_REMOTE="https://dl.google.com/go/${GOLANG_PRE}.tar.gz"
106 GOLANG_LOCAL="$my_tmp/${GOLANG_PRE}.tar.gz"
107 GOLANG_UNTAR="$my_tmp/${GOLANG_PRE}"
108
109 echo Downloading $GOLANG_REMOTE
110 if [ -n "$(command -v curl 2>/dev/null | grep curl)" ]; then
111   curl -fSL ${GOLANG_REMOTE} -o ${GOLANG_LOCAL} || echo 'error downloading golang'
112 elif [ -n "$(command -v wget 2>/dev/null | grep wget)" ]; then
113   wget ${GOLANG_REMOTE} -O ${GOLANG_LOCAL} || echo 'error downloading golang'
114 else
115   echo "'wget' and 'curl' are missing. Please run the following command and try again"
116   echo "    sudo apt-get install --yes curl wget"
117   exit 1
118 fi
119
120 echo Installing $GOLANG_LOCAL
121 mkdir -p ${GOLANG_UNTAR}/
122 # --strip-components isn't portable, switch to portable version by performing move step after untar
123 tar xf ${GOLANG_LOCAL} -C ${GOLANG_UNTAR}/ #--strip-components=1
124 mv ${GOLANG_UNTAR}/go/* ${GOLANG_UNTAR}/
125 rm -rf ${GOLANG_UNTAR}/go # clean up the temporary unzip folder
126 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
127   echo $sudo_cmd rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/"
128   rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/"
129 else
130   echo $sudo_cmd cp -Hr "${GOLANG_UNTAR}/*" "$golang_install_path/"
131   cp -Hr "${GOLANG_UNTAR}"/* "$golang_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${GOLANG_UNTAR}"/* "$golang_install_path/"
132 fi
133 rm -rf "${GOLANG_UNTAR}"
134
135 #######
136 # END #
137 #######
138
139 # TODO add more than one at a time
140 pathman add $golang_install_path/bin
141 mkdir -p $HOME/go/bin
142 pathman add $HOME/go/bin
143 echo "go get golang.org/x/tools/cmd/goimports"
144 $golang_install_path/bin/go get golang.org/x/tools/cmd/goimports > /dev/null 2>/dev/null