add examples
[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=${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 get_golang_version
85
86 #
87 # golang
88 #
89 golang_install_path=$HOME/.local/opt/${GOLANG_VER}
90 mkdir -p $golang_install_path
91
92 # TODO warn if existing golang in path my take precedence
93 if [ -e "$golang_install_path/bin/go" ]; then
94   # golang of some version is already installed
95   #echo "${GOLANG_VER}" == "$($golang_install_path/bin/go version | cut -d ' ' -f 3 2>/dev/null)"
96   if [ "${GOLANG_VER}" == "$($golang_install_path/bin/go version | cut -d ' ' -f 3 2>/dev/null)" ]; then
97     echo ${GOLANG_VER} already installed at $golang_install_path
98     exit 0
99   fi
100 fi
101
102 GOLANG_PRE="${GOLANG_VER}.${GOLANG_OS}-${GOLANG_ARCH}"
103 GOLANG_REMOTE="https://dl.google.com/go/${GOLANG_PRE}.tar.gz"
104 GOLANG_LOCAL="$my_tmp/${GOLANG_PRE}.tar.gz"
105 GOLANG_UNTAR="$my_tmp/${GOLANG_PRE}"
106
107 if [ -n "$(command -v curl 2>/dev/null | grep curl)" ]; then
108   curl -fSL ${GOLANG_REMOTE} -o ${GOLANG_LOCAL} || echo 'error downloading golang'
109 elif [ -n "$(command -v wget 2>/dev/null | grep wget)" ]; then
110   wget ${GOLANG_REMOTE} -O ${GOLANG_LOCAL} || echo 'error downloading golang'
111 else
112   echo "'wget' and 'curl' are missing. Please run the following command and try again"
113   echo "    sudo apt-get install --yes curl wget"
114   exit 1
115 fi
116
117 mkdir -p ${GOLANG_UNTAR}/
118 # --strip-components isn't portable, switch to portable version by performing move step after untar
119 tar xf ${GOLANG_LOCAL} -C ${GOLANG_UNTAR}/ #--strip-components=1
120 mv ${GOLANG_UNTAR}/go/* ${GOLANG_UNTAR}/
121 rm -rf ${GOLANG_UNTAR}/go # clean up the temporary unzip folder
122 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
123   echo $sudo_cmd rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/"
124   rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${GOLANG_UNTAR}/" "$golang_install_path/"
125 else
126   echo $sudo_cmd cp -Hr "${GOLANG_UNTAR}/*" "$golang_install_path/"
127   cp -Hr "${GOLANG_UNTAR}"/* "$golang_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${GOLANG_UNTAR}"/* "$golang_install_path/"
128 fi
129 rm -rf "${GOLANG_UNTAR}"
130
131 #######
132 # END #
133 #######
134
135 # TODO add more than one at a time
136 pathman add $golang_install_path/bin
137 mkdir -p $HOME/go/bin
138 pathman add $HOME/go/bin
139 echo "go get golang.org/x/tools/cmd/goimports"
140 $golang_install_path/bin/go get golang.org/x/tools/cmd/goimports > /dev/null 2>/dev/null