Skip to content

Instantly share code, notes, and snippets.

@phemmer
Created November 26, 2016 21:27
Show Gist options
  • Save phemmer/81922b32aa61b6707bd9940a2f11a1e4 to your computer and use it in GitHub Desktop.
Save phemmer/81922b32aa61b6707bd9940a2f11a1e4 to your computer and use it in GitHub Desktop.
GDM wrapper for not mucking with shared $GOPATH
#!/bin/bash
self_path="$(readlink -f "$0")"
self_dir="${self_path%/*}"
#unset PWD # screws with go package path detection
IFS=$'\n' read -d '' -a gdms < <(which -a gdm)
realgdm=
foundself=
for gdm in "${gdms[@]}"; do
if [[ -n "$foundself" ]]; then
realgdm="$gdm"
break
fi
if [[ "$(readlink -f "$gdm")" == "$self_path" ]]; then
foundself=1
fi
done
if [[ -z "$realgdm" ]]; then
echo "gdm not found" >&2
exit 1
fi
projdir=$PWD
while [[ ! -e $projdir/Godeps ]] && [[ "$projdir" != "" ]]; do
projdir=${projdir%/*}
done
if [[ "$projdir" == "" ]]; then
projdir=$PWD
fi
import_path=$(cd $projdir && go list -f '{{.ImportPath}}' .)
mkdir -p "$projdir/.go/src/${import_path%/*}"
ln -sf "$projdir" "$projdir/.go/src/${import_path}"
twd=/tmp/.gdm_$$
ln -sf "$projdir/.go" $twd
trap "rm -f $twd" EXIT
GOPATH="$twd:$GOPATH"
cd "$twd/src/$import_path/${PWD#$projdir}"
if [[ "$1" == exec ]]; then
shift
"$@"
exit $?
fi
$realgdm "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment