422b0ebf4b1d70c0c4a8f074c5effbc6feeda7ff
[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 GOBIN="${HOME}/go"
39 GOBIN_REAL="${HOME}/.local/opt/go-bin-v${WEBI_VERSION}"
40
41 # The package is 'golang', but the command is 'go'
42 pkg_cmd_name="go"
43
44 # NOTE: pkg_* variables can be defined here
45 #       pkg_cmd_name
46 #       pkg_new_opt, pkg_new_bin, pkg_new_cmd
47 #       pkg_common_opt, pkg_common_bin, pkg_common_cmd
48 #
49 # Their defaults are defined in webi/template.bash at https://github.com/webinstall/packages
50
51 pkg_get_current_version() {
52     # 'go version' has output in this format:
53     #       go version go1.14.2 darwin/amd64
54     # This trims it down to just the version number:
55     #       1.14.2
56     echo "$(go version | cut -d' ' -f3 | sed 's:go::')"
57 }
58
59 pkg_link_new_version() {
60     # 'pkg_common_opt' will default to $HOME/.local/opt/go
61     # 'pkg_new_opt' will be the installed version, such as to $HOME/.local/opt/go-v1.14.2
62     rm -rf "$pkg_common_opt"
63     ln -s "$pkg_new_opt" "$pkg_common_opt"
64
65     # Go has a special $GOBIN
66
67     # 'GOBIN' is set above to "${HOME}/go"
68     # 'GOBIN_REAL' will be "${HOME}/.local/opt/go-bin-v${WEBI_VERSION}"
69     rm -rf "$GOBIN"
70     mkdir -p "$GOBIN_REAL"
71     ln -s "$GOBIN_REAL" "$GOBIN"
72 }
73
74 pkg_pre_install() {
75     # web_* are defined in webi/template.bash at https://github.com/webinstall/packages
76
77     # multiple versions may be installed
78     # if one already matches, it will simply be re-linked
79     webi_check
80
81     # the download is quite large - hopefully you have wget installed
82     # will go to ~/Downloads by default
83     webi_download
84
85     # Multiple formats are supported: .xz, .tar.*, and .zip
86     # will be extracted to $WEBI_TMP
87     webi_extract
88 }
89
90 pkg_install() {
91     pushd "$WEBI_TMP" 2>&1 >/dev/null
92
93         # remove the versioned folder, just in case it's there with junk
94         rm -rf "$pkg_new_opt"
95
96         # rename the entire extracted folder to the new location
97         # (this will be "$HOME/.local/opt/go-v$WEBI_VERSION" by default)
98         mv ./go* "$pkg_new_opt"
99
100     popd 2>&1 >/dev/null
101 }
102
103 pkg_post_install() {
104     pkg_link_new_version
105
106     # web_path_add is defined in webi/template.bash at https://github.com/webinstall/packages
107     # Updates PATH with
108     #       "$HOME/.local/opt/go"
109     webi_path_add "$pkg_common_bin"
110     webi_path_add "$GOBIN"
111
112     # Install x go
113     "$pkg_common_cmd" get golang.org/x/tools/cmd/goimports > /dev/null 2>/dev/null
114     "$pkg_common_cmd" get golang.org/x/tools/cmd/gorename > /dev/null 2>/dev/null
115     "$pkg_common_cmd" get golang.org/x/tools/cmd/gotype > /dev/null 2>/dev/null
116     "$pkg_common_cmd" get golang.org/x/tools/cmd/stringer > /dev/null 2>/dev/null
117 }
118
119 pkg_post_install_message() {
120     echo "Installed 'go' (and go tools)"
121 }