a6a8592ca6d76febbd7c59d5fd44268d99b9cc1f
[webi-installers/.git] / golang / install.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 ###################
41 # Install go #
42 ###################
43
44 common_go_home="${HOME}/.local/opt/go-v${WEBI_VERSION}"
45 new_go_home="${HOME}/.local/opt/go-v${WEBI_VERSION}"
46 new_go="${HOME}/.local/opt/go-v${WEBI_VERSION}/bin/go"
47
48 update_go_home() {
49     rm -rf "$common_go_home"
50     ln -s "$new_go_home" "$common_go_home"
51
52     # TODO get better output from pathman / output the path to add as return to webi bootstrap
53     webi_path_add "$common_go_home/bin"
54     webi_path_add "$HOME/go/bin"
55 }
56
57 if [ -x "$new_go_home/bin/go" ]; then
58   update_go_home
59   exit 0
60 fi
61
62 # Test for existing version
63 set +e
64 cur_go="$(command -v go)"
65 set -e
66 if [ -n "$cur_go" ]; then
67   cur_ver=$(go version | cut -d' ' -f3 | sed 's:go::')
68   if [ "$cur_ver" == "$(echo $WEBI_VERSION | sed 's:\.0::g')" ]; then
69     echo "go v$WEBI_VERSION already installed at $cur_go"
70     exit 0
71   elif [ "$cur_go" != "$new_go" ]; then
72     echo "WARN: possible conflict with go v$WEBI_VERSION at $cur_go"
73   fi
74 fi
75
76
77 # Note: this file is `source`d by the true installer and hence will have the webi functions
78
79 # because we created releases.js we can use webi_download()
80 # downloads go to ~/Downloads
81 webi_download
82
83 # because this is tar or zip, we can webi_extract()
84 # extracts to the WEBI_TMP directory, raw (no --strip-prefix)
85 webi_extract
86
87 pushd "$WEBI_TMP" 2>&1 >/dev/null
88     echo Installing go v${WEBI_VERSION} as "$new_go"
89
90     # simpler for single-binary commands
91     #mv ./example*/bin/example "$HOME/.local/bin"
92
93     # best for packages and toolchains
94     rm -rf "$new_go_home"
95     if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
96       rsync -Krl ./go*/ "$new_go_home/" 2>/dev/null
97     else
98       cp -Hr ./go*/* "$new_go_home/" 2>/dev/null
99       cp -Hr ./go*/.* "$new_go_home/" 2>/dev/null
100     fi
101     rm -rf ./go*
102
103     # Install x go
104     $new_go_home/bin/go get golang.org/x/tools/cmd/goimports > /dev/null 2>/dev/null
105     $new_go_home/bin/go get golang.org/x/tools/cmd/gorename > /dev/null 2>/dev/null
106     $new_go_home/bin/go get golang.org/x/tools/cmd/gotype > /dev/null 2>/dev/null
107     $new_go_home/bin/go get golang.org/x/tools/cmd/stringer > /dev/null 2>/dev/null
108 popd 2>&1 >/dev/null
109
110 ###################
111 #   Update PATH   #
112 ###################
113
114 update_go_home
115
116 echo "Installed 'go' (and go tools)"
117 echo ""