Skip to content

Instantly share code, notes, and snippets.

@mafredri mafredri/brew-go-get.sh
Last active Nov 8, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.