Sunday, January 13, 2013

shell.function to download all Repositories from a GitHub account

a shell utility to download all repositories managed under any GitHub account in the current directory...
if the current directory has any of the repositories already cloned, it pulls the latest

usage:   $ clone-github github
would download all the repositories managed under 'github' account available at https://github.com/github

#!/bin/bash
# Usage Example: $ clone-github github_id
# would clone all the git repositories managed under github_id at current path
# save it as /etc/profiles/a.github.sh
##
clone-github(){
GITHUB_ID=$1
GITHUB_REPO_URI="https://github.com/"$GITHUB_ID"?tab=repositories"
repos=`curl -skL $GITHUB_REPO_URI | grep 'name codeRepository' | sed 's/.*href=\"//' | sed 's/".*//'`
for line in `echo $repos | xargs -L1`;
do
if [[ -z $line ]]; then
next
fi
repo_git='git://github.com'$line'.git'
repo_dir=$(basename $line)
if [[ -d "${repo_dir}" ]]; then
echo 'Fetching master latest pull for: '$repo_git
pushd "${repo_dir}" ; git pull ; popd
else
echo "Cloning... "$repo_git
git clone $repo_git
fi
done
}
view raw a.github.sh hosted with ❤ by GitHub
######## _library functions
######## can infer from 'setup-chruby' and 'setup-fzf' at end on how to utilize these
download-release-from-github(){
local REPO_PATH="$1"
local REPO_VERSION="$2"
local TAR_GZ="$3"
local GITHUB_DDL_HEADERS=$(curl -Iks "https://github.com/${REPO_PATH}/releases/download/${REPO_VERSION}/${TAR_GZ}")
local URLTO_DDL=$(echo "$GITHUB_DDL_HEADERS" | grep '^Location: ' | awk '{print $2}' | sed 's/[[:space:]]*$//g')
local HEADER_GITHUB_REQUEST_ID=$(echo "$GITHUB_DDL_HEADERS" | grep '^X-GitHub-Request-Id: ' | awk '{print $2}' | sed 's/[[:space:]]*$//g')
local HEADER_REQUEST_ID=$(echo "$GITHUB_DDL_HEADERS" | grep '^X-Request-Id: ' | awk '{print $2}' | sed 's/[[:space:]]*$//g')
curl -H "X-GitHub-Request-Id: ${HEADER_GITHUB_REQUEST_ID}" -H "X-Request-Id: ${HEADER_REQUEST_ID}" -Lk -o "${TAR_GZ}" "${URLTO_DDL}"
}
download-archive-from-github(){
local REPO_PATH="$1"
local REPO_VERSION="$2"
local TAR_GZ="$3"
}
setup-from-github(){
local REPO_PATH="$1"
local REPO_VERSION="$2"
local TAR_GZ="$3"
local SETUP_CMD="$4"
local RELEASE_OR_ARCHIVE=${5:-release}
set -ex
pushd /tmp
if [[ "${RELEASE_OR_ARCHIVE}" == "release" ]]; then
download-release-from-github "${REPO_PATH}" "${REPO_VERSION}" "${TAR_GZ}"
elif [[ "${RELEASE_OR_ARCHIVE}" == "archive" ]]; then
local TARBALL_URI="https://github.com/${REPO_PATH}/archive/${TAR_GZ}"
wget -c -O "${TAR_GZ}" "${TARBALL_URI}"
else
echo "[error] unidentified github download type"
fi
tar zxvf "${TAR_GZ}"
eval "${SETUP_CMD}"
rm "${TAR_GZ}"
popd
set +ex
}
###################### _examples :: release uploads
setup-fzf(){
local REPO_PATH="junegunn/fzf-bin"
local REPO_VERSION="0.17.5"
local TAR_GZ="fzf-0.17.5-linux_amd64.tgz"
local SETUP_CMD="mv ./fzf ${HOME_BINDIR}/fzf"
[[ $(fzf --version &>/dev/null ; echo $?) -eq 0 ]] && \
echo "* fzf is already setup" && \
return 0
setup-from-github "${REPO_PATH}" "${REPO_VERSION}" "${TAR_GZ}" "${SETUP_CMD}"
}
###################### _examples :: release source code archives
setup-chruby(){
local CHRUBY_VERSION="0.3.9"
local CHRUBY_DIR="chruby-${CHRUBY_VERSION}"
local FILE_TO_SOURCE="/usr/local/share/chruby/chruby.sh"
local REPO_PATH="postmodern/chruby"
local REPO_VERSION="v${CHRUBY_VERSION}"
local TAR_GZ="${REPO_VERSION}.tar.gz"
local SETUP_CMD="pushd \"${CHRUBY_DIR}\" ; sudo ./scripts/setup.sh ; popd ; rm -rf \"${CHRUBY_DIR}\""
[[ -f "${FILE_TO_SOURCE}" ]] && \
echo "* chruby is already setup" && \
return 0
setup-from-github "${REPO_PATH}" "${REPO_VERSION}" "${TAR_GZ}" "${SETUP_CMD}" "archive"
source "${FILE_TO_SOURCE}"
}

No comments:

Post a Comment