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