Skip to content

Commit 0b661fd

Browse files
committed
fix(go): Update user facing utility scripts
1 parent 1348791 commit 0b661fd

3 files changed

Lines changed: 203 additions & 274 deletions

File tree

src/go/bin/go-install.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
# This script upgrades Go to the specified version.
3+
# Usage: ./go-install.sh <version>
4+
# Example: ./go-install.sh go1.20.5
5+
# Note: Ensure that the GOROOT environment variable is set to the desired
6+
# installation path before running this script.
7+
set -euo pipefail
8+
9+
install_go() {
10+
local go_version="${1:?Version argument is required}"
11+
local go_root="${GOROOT:?Environment variable GOROOT must be set}"
12+
13+
local go_arch
14+
go_arch=$(uname -m)
15+
case $go_arch in
16+
x86_64) go_arch="amd64" ;;
17+
aarch64 | armv8*) go_arch="arm64" ;;
18+
i?86) go_arch="386" ;;
19+
*)
20+
echo "Error: Unsupported architecture '$go_arch'. Supported architectures are x86_64 (amd64), aarch64 (arm64), and i386 (386)."
21+
exit 1
22+
;;
23+
esac
24+
local download_url="https://go.dev/dl/${go_version}.linux-${go_arch}.tar.gz"
25+
26+
echo "Removing any existing Go installation"
27+
rm -rfv "$go_root"
28+
echo "Downloading and extracting Go from $download_url to $(dirname "$go_root")..."
29+
curl -sSL "$download_url" | tar -C "$(dirname "$go_root")" -xz
30+
31+
echo "Go $go_version installed to $go_root."
32+
echo "(*) Please ensure that $go_root/bin is in your PATH."
33+
}
34+
35+
if [ "$#" -ne 1 ]; then
36+
echo "Usage: $0 <version>"
37+
echo "Example: $0 go1.20.5"
38+
echo "See https://go.dev/dl/ and https://github.com/golang/go/tags for available versions."
39+
exit 1
40+
fi
41+
42+
install_go "$1"

src/go/bin/go-tools-install.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
# This script installs Go tools specified in a tools.txt file.
3+
# Usage: ./go-tools-install.sh <path/to/tools.txt>
4+
# Example: ./go-tools-install.sh /usr/local/share/go-tools.txt
5+
6+
set -euo pipefail
7+
8+
make_bin_completions() {
9+
local _completion_dir_bash="/etc/bash_completion.d"
10+
local _completion_dir_zsh="/usr/share/zsh/site-functions"
11+
local _completion_dir_fish="/usr/share/fish/vendor_completions.d"
12+
echo "::Detected $1, generating completions..."
13+
local bin_name="$1"
14+
mkdir -pv "$_completion_dir_bash" "$_completion_dir_zsh" "$_completion_dir_fish"
15+
"$GOBIN/$bin_name" completion bash >"$_completion_dir_bash/$bin_name"
16+
"$GOBIN/$bin_name" completion zsh >"$_completion_dir_zsh/_$bin_name"
17+
"$GOBIN/$bin_name" completion fish >"$_completion_dir_fish/$bin_name.fish"
18+
echo "$bin_name completion scripts installed."
19+
}
20+
21+
install_go_tools() {
22+
local go_toolstxt_path="${1:?Path to tools.txt is required}"
23+
local go_tools
24+
go_tools=$(cat "$go_toolstxt_path")
25+
echo "Tools to be installed:"
26+
xargs -I{} -n 1 echo " - {}" <<<"$go_tools"
27+
[[ -z "${GOBIN:-}" ]] && {
28+
echo "Error: GOBIN environment variable must be set."
29+
exit 1
30+
}
31+
xargs -n 1 go install -v <<<"$go_tools"
32+
echo "Go tools installed (via go install)."
33+
if grep -xqE "github.com/golangci/golangci-lint/(v[0-9]+/)?cmd/golangci-lint@([a-zA-Z0-9._-]+)?" <<<"$go_tools"; then
34+
make_bin_completions "golangci-lint"
35+
fi
36+
if grep -xqE "github.com/spf13/cobra-cli@([a-zA-Z0-9._-]+)?" <<<"$go_tools"; then
37+
make_bin_completions "cobra-cli"
38+
fi
39+
}
40+
41+
if [ "$#" -ne 1 ]; then
42+
echo "Usage: $0 <path/to/tools.txt>"
43+
echo "Example: $0 /usr/local/share/go-tools.txt"
44+
exit 1
45+
fi
46+
47+
install_go_tools "$1"

0 commit comments

Comments
 (0)