Last active
August 29, 2015 14:08
-
-
Save rjeczalik/db886c79049d83f318e2 to your computer and use it in GitHub Desktop.
Imports all Go projects from your GitHub account into a GOPATH workspace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Rafal Jeczalik <[email protected]> | |
# https://gist.github.com/rjeczalik/db886c79049d83f318e2 | |
type curl 1>/dev/null || exit | |
type jq 1>/dev/null || exit | |
die_usage() { | |
if [ ! -z "${*}" ]; then | |
echo "${*}" 1>&2 | |
echo | |
fi | |
echo "usage: gh-gopath USER NEW_GOPATH" 1>&2 | |
exit 1 | |
} | |
USR=${1} | |
WRK=${2} | |
[ -z "$USR" ] && die_usage "user name cannot be empty" | |
[ -z "$WRK" ] && die_usage "new \$GOPATH cannot be empty" | |
[ -d "$WRK" ] && die_usage "path \"${2}\" already exists" | |
CURL='curl -s' | |
if [ ! -z "$GITHUB_TOKEN" ]; then | |
CURL="$CURL -u $GITHUB_TOKEN:x-oauth-basic" | |
fi | |
$CURL "https://api.github.com/user" | jq '.message' 2>/dev/null | tr -d \" | grep 'API rate limit exceeded' 1>&2 && | |
echo && echo "HINT: export GITHUB_TOKEN with your access token (Settings -> Applications -> Personal access tokens)" && exit | |
git init "$WRK" >/dev/null; cd "$WRK" | |
look_upstream() { | |
$CURL "https://api.github.com/search/repositories?q=${1#*/}&language=Go" | jq ".items | .[] | if (.name == \"${1#*/}\" and .fork == false) then .full_name else empty end" | tr -d \" | | |
while read REPO; do | |
N=$(( $($CURL "https://api.github.com/repos/$REPO" | jq '.forks_count') / 100 + 1)) | |
for ((i=0; i<$N; i++)); do | |
UPSTREAM=$($CURL "https://api.github.com/repos/$REPO/forks?per_page=100&page=$i" | jq ".[] | if .full_name == \"$1\" then \"$REPO\" else empty end" | tr -d \") | |
if [ ! -z "$UPSTREAM" ]; then | |
echo $UPSTREAM; return | |
fi | |
done | |
done | |
} | |
$CURL "https://api.github.com/users/$USR/repos" | jq '.[] | if .language == "Go" then {full_name, fork} else empty end | if .fork then .full_name+" 1" else .full_name+" 0" end' | tr -d \" | | |
while read ORIGIN FORK; do | |
[email protected]:$ORIGIN.git | |
SRC=src/github.com/$ORIGIN | |
if [ $FORK -eq 1 ]; then | |
UPSTREAM=$(look_upstream "$ORIGIN") | |
if [ ! -z "${UPSTREAM}" ]; then | |
SRC=src/github.com/$UPSTREAM | |
else | |
echo "# failed to find upstream for $ORIGIN" 1>&2 | |
continue | |
fi | |
fi | |
if git submodule add $REMOTE $SRC &>/dev/null; then | |
echo -e "$ORIGIN\t->\t\$GOPATH/$SRC" | |
else | |
echo "# failed to clone [email protected]:$ORIGIN.git" 1>&2 | |
fi | |
done |
Author
rjeczalik
commented
Oct 27, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment