Skip to content

Instantly share code, notes, and snippets.

@mafredri
Last active August 29, 2021 17:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mafredri/da45685b4d0a9a630ee7c7dc355a9f87 to your computer and use it in GitHub Desktop.
Save mafredri/da45685b4d0a9a630ee7c7dc355a9f87 to your computer and use it in GitHub Desktop.
Homebrew managed go get
#!/bin/bash
# brew-go-get
# Original: https://blog.filippo.io/cleaning-up-my-gopath-with-homebrew/
set -euo pipefail
name_to_repo() {
local name="$1"
name="${name/go-get-}"
echo "${name//--//}"
}
repo_to_name() {
local repo="$1"
echo "go-get-${repo//\//--}"
}
install() {
local pkg="$1"; shift
local args=("$@")
local name version
name="$pkg"
version=$(date '+%Y-%m-%d')
if [[ $name = go-get-* ]]; then
pkg="$(name_to_repo $name)"
else
name="$(repo_to_name $name)"
fi
export GOPATH="$(brew --prefix)/Cellar/$name/$version"
go get ${args[@]} "$pkg"
rm -rf "$GOPATH"/{pkg,src}
brew switch "$name" "$version" 2>/dev/null || {
brew unlink "$name" 2> /dev/null || true
brew link "$name"
}
}
if [[ $# -lt 1 ]] || [[ $* =~ -h|--help ]] ; then
echo "Usage: brew-go-get [upgrade] <github.com/foo/bar|go-get-github.com--foo--bar> <...>"
exit 1
fi
ARGS=("")
if [[ $1 = upgrade ]]; then
ARGS+=(-u)
shift
fi
for pkg in "$@"; do
install "$pkg" "${ARGS[@]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment