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