fixes and cleanup
[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 # Use the script's first argument or the supplied WEBI_VERSION or ''
41 WEBI_VERSION=${1:-${WEBI_VERSION:-}}
42
43 # Set a temporary directory, if not already set
44 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-go.XXXXXXXX)"}
45
46 ###################
47 #  Get WEBI vars  #
48 ###################
49
50 # The WEBI bootstrap will define these
51 # but each script should be testable in its own right
52
53 if [ -z "${WEBI_PKG_URL:-}" ]; then
54   release_tab="${WEBI_HOST}/api/releases/golang@${WEBI_VERSION:-}.csv?os=$(uname -s)&arch=$(uname -m)&ext=tar&limit=1"
55   WEBI_CSV=$(curl -fsSL "$release_tab" -H "User-Agent: $(uname -a)")
56   WEBI_CHANNEL=$(echo $WEBI_CSV | cut -d ',' -f 3)
57   if [ "error" == "$WEBI_CHANNEL" ]; then
58      echo "could not find release for go v${WEBI_VERSION}"
59      exit 1
60   fi
61   WEBI_VERSION=$(echo $WEBI_CSV | cut -d ',' -f 1)
62   WEBI_PKG_URL=$(echo $WEBI_CSV | cut -d ',' -f 9)
63   WEBI_PKG_FILE="$WEBI_TMP/$(echo $WEBI_PKG_URL | sed s:.*/::)"
64 fi
65
66 ###################
67 # Install go #
68 ###################
69
70 new_go_home="${HOME}/.local/opt/go-v${WEBI_VERSION}"
71 new_go="${HOME}/.local/opt/go-v${WEBI_VERSION}/bin/go"
72
73 # Test for existing version 
74 set +e
75 cur_go="$(command -v go)"
76 set -e
77 if [ -n "$cur_go" ]; then
78   # TODO this is still sometimes wrong (i.e. 1.14 = 1.14.0)
79   cur_ver=$(go version | cut -d' ' -f3 | sed 's:go::')
80   if [ "$cur_ver" == "$(echo $WEBI_VERSION | sed 's:\.0::g')" ]; then
81     echo "go v$WEBI_VERSION already installed at $cur_go"
82     exit 0
83   elif [ "$cur_go" != "$new_go" ]; then
84     echo "WARN: possible conflict with go v$WEBI_VERSION at $cur_go"
85   fi
86 fi
87
88 # TODO move download to the webi bootstrap
89 echo Downloading go v"${WEBI_VERSION}" from "${WEBI_PKG_URL}"
90 curl -fsSL "${WEBI_PKG_URL}" -o "${WEBI_PKG_FILE}"
91 pushd "${WEBI_TMP}" 2>&1 >/dev/null
92         echo Installing go v${WEBI_VERSION} as "$new_go" 
93         tar xf "${WEBI_PKG_FILE}"
94         rm "${WEBI_PKG_FILE}"
95
96         # simpler for single-binary commands
97         #mv ./example*/bin/example "$HOME/.local/bin"
98
99         # best for packages and toolchains
100         rm -rf "$new_go_home"
101         if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
102           rsync -Krl ./go*/ "$new_go_home/" 2>/dev/null
103         else
104           cp -Hr ./go*/* "$new_go_home/" 2>/dev/null
105           cp -Hr ./go*/.* "$new_go_home/" 2>/dev/null
106         fi
107
108         # Install x go
109         $new_go_home/bin/go get golang.org/x/tools/cmd/goimports > /dev/null 2>/dev/null
110         $new_go_home/bin/go get golang.org/x/tools/cmd/gorename > /dev/null 2>/dev/null
111         $new_go_home/bin/go get golang.org/x/tools/cmd/gotype > /dev/null 2>/dev/null
112         $new_go_home/bin/go get golang.org/x/tools/cmd/stringer > /dev/null 2>/dev/null
113 popd 2>&1 >/dev/null
114
115 ###################
116 #   Update PATH   #
117 ###################
118
119 # TODO get better output from pathman / output the path to add as return to webi bootstrap
120 pathman add "$new_go_home/bin"
121 pathman add "$HOME/go/bin"
122 echo "Installed 'go' (and go tools)"
123 echo ""