update variables _new_ => _src_, _common_ => _dst_,
[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_src, pkg_src_bin, pkg_src_cmd
47 #       pkg_dst, pkg_dst_bin, pkg_dst_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 >2/dev/null | head -n 1 | cut -d' ' -f3 | sed 's:go::')"
57 }
58
59 pkg_format_cmd_version() {
60     # 'go v1.14.0' will be 'go1.14'
61     my_version=$(echo "$1" | sed 's:\.0::g')
62     echo "${pkg_cmd_name}${my_version}"
63 }
64
65 pkg_link_src_dst() {
66     # 'pkg_dst' will default to $HOME/.local/opt/go
67     # 'pkg_src' will be the installed version, such as to $HOME/.local/opt/go-v1.14.2
68     rm -rf "$pkg_dst"
69     ln -s "$pkg_src" "$pkg_dst"
70
71     # Go has a special $GOBIN
72
73     # 'GOBIN' is set above to "${HOME}/go"
74     # 'GOBIN_REAL' will be "${HOME}/.local/opt/go-bin-v${WEBI_VERSION}"
75     rm -rf "$GOBIN"
76     mkdir -p "$GOBIN_REAL/bin"
77     ln -s "$GOBIN_REAL" "$GOBIN"
78 }
79
80 pkg_pre_install() {
81     # web_* are defined in webi/template.bash at https://github.com/webinstall/packages
82
83     # multiple versions may be installed
84     # if one already matches, it will simply be re-linked
85     webi_check
86
87     # the download is quite large - hopefully you have wget installed
88     # will go to ~/Downloads by default
89     webi_download
90
91     # Multiple formats are supported: .xz, .tar.*, and .zip
92     # will be extracted to $WEBI_TMP
93     webi_extract
94 }
95
96 pkg_install() {
97     pushd "$WEBI_TMP" 2>&1 >/dev/null
98
99         # remove the versioned folder, just in case it's there with junk
100         rm -rf "$pkg_src"
101
102         # rename the entire extracted folder to the new location
103         # (this will be "$HOME/.local/opt/go-v$WEBI_VERSION" by default)
104         mv ./"$pkg_cmd_name"* "$pkg_src"
105
106     popd 2>&1 >/dev/null
107 }
108
109 pkg_post_install() {
110     pkg_link_src_dst
111
112     # web_path_add is defined in webi/template.bash at https://github.com/webinstall/packages
113     # Updates PATH with
114     #       "$HOME/.local/opt/go"
115     webi_path_add "$pkg_dst_bin"
116     webi_path_add "$GOBIN/bin"
117
118     # Install x go
119     echo "Installing go extended tools (goimports, gorename, etc)"
120     "$pkg_dst_cmd" get golang.org/x/tools/cmd/goimports > /dev/null 2>/dev/null
121     "$pkg_dst_cmd" get golang.org/x/tools/cmd/gorename > /dev/null 2>/dev/null
122     "$pkg_dst_cmd" get golang.org/x/tools/cmd/gotype > /dev/null 2>/dev/null
123     "$pkg_dst_cmd" get golang.org/x/tools/cmd/stringer > /dev/null 2>/dev/null
124 }
125
126 pkg_post_install_message() {
127     echo "Installed 'go' (and go tools)"
128 }