|
| 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