Skip to content

Instantly share code, notes, and snippets.

@Nitecon
Last active June 29, 2016 15:31
Show Gist options
  • Save Nitecon/3ba84364f1d57a76aa5d to your computer and use it in GitHub Desktop.
Save Nitecon/3ba84364f1d57a76aa5d to your computer and use it in GitHub Desktop.
gov provides a way to create vendor experiment setups directly (Requires mac / linux)
#!/bin/bash
GOSRC="$GOPATH/src/"
CURPATH=`pwd`
if [ -z "$GOPATH" ]; then
echo "Need to set GOPATH"
exit 1
fi
if ! [[ $GOPATH == */ ]]; then
GOPATH=$GOPATH/
fi
if ! [[ $PWD == ${GOPATH}src/* ]]; then
echo Must be in '$GOPATH/src/<projname>' to use vendoring
exit 1
fi
if [[ $PWD == *"vendor"* ]]; then
echo "Cannot do vendoring inside vendor"
exit 1
fi
declare -a UPDATEDPACKAGES
array_contains () {
local array="$1[@]"
local seeking=$2
local in=1
for element in "${!array}"; do
if [[ $element == $seeking ]]; then
in=0
break
fi
done
return $in
}
GOLEN=${#GOSRC}
PACKLEN=${#CURPATH}
MaxLEN=`expr $PACKLEN - $GOLEN`
CURPACKAGE=${CURPATH:GOLEN:MaxLEN}
DEPLIST=`go list -f '{{join .Deps "\n"}}' | xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}'|grep -v $CURPACKAGE`
for PNAME in $DEPLIST; do
IFS='/' read -ra i <<< "$PNAME"
DEPPACKAGE="${i[0]}/${i[1]}/${i[2]}"
REPODOMAIN=${i[0]}
OWNERNAME=${i[1]}
REPONAME=${i[2]}
if [ -d $GOPATH/src/$DEPPACKAGE ]; then
if [ "$REPODOMAIN" != "git.nexgen.neustar.biz" ]; then
if [ ! -d $CURPATH/vendor/$DEPPACKAGE ]; then
mkdir -p $CURPATH/vendor/$DEPPACKAGE
echo "The DEP Package to be created is $DEPPACKAGE"
rsync -a --del --exclude=.git --exclude=vendor $GOPATH/src/$DEPPACKAGE/ $CURPATH/vendor/$DEPPACKAGE/
fi
else
NEEDSUPDATE="yes"
array_contains UPDATEDPACKAGES "$DEPPACKAGE" && NEEDSUPDATE="yes" || NEEDSUPDATE="no"
if [ $NEEDSUPDATE == "no" ]; then
echo "Updating $DEPPACKAGE"
CHANGES=`git status -s |grep M`
cd $GOPATH/src/$DEPPACKAGE
if [ -z CHANGES ]; then
echo "Changes detected for $DEPPACKAGE, stashing pulling and applying"
git stash
git pull
git stash apply
UPDATEDPACKAGES+=("$DEPPACKAGE")
cd $CURPATH
else
git pull
UPDATEDPACKAGES+=("$DEPPACKAGE")
cd $CURPATH
fi
fi
fi
#Package is available so lets see if it exists and if not pull it / sync
else
if [ "$REPODOMAIN" != "git.nexgen.neustar.biz" ]; then
go get $DEPPACKAGE
mkdir -p $CURPATH/vendor/$DEPPACKAGE
echo "The DEP Package that exists is $DEPPACKAGE... syncing it to vendor"
rsync -a --del --exclude=.git --exclude=vendor $GOPATH/src/$DEPPACKAGE/ $CURPATH/vendor/$DEPPACKAGE/
else
echo "Cloning non existent nexgen package: $OWNERNAME/$REPONAME"
mkdir -p $GOPATH/src/$DEPPACKAGE
cd $GOPATH/src/$DEPPACKAGE
git clone ssh://git@git.nexgen.neustar.biz:8022/$OWNERNAME/$REPONAME.git
UPDATEDPACKAGES+=("$DEPPACKAGE")
fi
fi
done
UPDATE=""
if [ "$1" == "--update" ]; then
UPDATE="-u"
fi
if [ -d vendor ]; then
for dist in `ls $CURPATH/vendor/`; do
if [ "$dist" != "git.nexgen.neustar.biz" ]; then
for owner in `ls $CURPATH/vendor/$dist/`; do
if [ "$dist" == "gopkg.in" ]; then
if [[ "$2" != "" && "$2" != "$owner" && "$2" != "`ls $CURPATH/vendor/$dist/$owner/`" ]]; then
continue
fi
echo "Upgrading $dist/$owner"
go get $UPDATE $dist/$owner
rsync -a --del --exclude=.git --exclude=vendor $GOPATH/src/$dist/$owner/ $CURPATH/vendor/$dist/$owner/
#cd $CURPATH/vendor/$dist/$owner && git pull
else
for pack in `ls $CURPATH/vendor/$dist/$owner/`; do
if [[ "$2" != "" && "$2" != "$pack" ]]; then
continue
fi
echo "Upgrading $dist/$owner/$pack"
go get $UPDATE $dist/$owner/$pack
rsync -a --del --exclude=.git --exclude=vendor $GOPATH/src/$dist/$owner/$pack/ $CURPATH/vendor/$dist/$owner/$pack/
#cd $CURPATH/vendor/$dist/$owner/$pack && git pull
done
fi
done
fi
done
else
echo "This is not a vendored go app."
fi
@Nitecon
Copy link
Author

Nitecon commented Dec 4, 2015

As an afterthought by one of my colleagues this should probably be called gv... although I prefer gov... cause why not govna

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment