-
-
Save ChristopherA/674125a7b83aaae04aa7d2faa5f1e303 to your computer and use it in GitHub Desktop.
Contains function to work with github repo and gists
This file contains 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
#! env zsh | |
######################################## | |
# git_helper.zsh | |
# Author: Ajeet Singh | |
# provides functions as commands for git | |
######################################## | |
#variables | |
export GIST_UPLOAD_URL="https://api.github.com/gists" | |
export GIST_DOWNLOAD_URL="https://gist.github.com" | |
export GIST_TOKEN="============================" | |
export GITHUB_TOKEN="${GIST_TOKEN}:x-oauth-basic" | |
export GITHUB_USER="singajeet" | |
export GIST_INDEX_URL="https://api.github.com/users/${GITHUB_USER}/gists" | |
function gist_usage(){ | |
echo "" | |
echo "Gist command line usage" | |
echo "=======================" | |
echo "" | |
echo "To list all gists under your id" | |
echo "" | |
echo "$ gist -l" | |
echo "" | |
echo "To download gist as file:" | |
echo "" | |
echo "$ gist -d [gist id] [out filename]" | |
echo "" | |
echo "To upload a file as gist:" | |
echo "" | |
echo "$ gist [filename] [file description]" | |
echo "" | |
} | |
function gist_list(){ | |
curl ${GIST_INDEX_URL} | jq '.[] | {id: .id, filename: .files[].filename}' | |
} | |
function gist_download(){ | |
GIST_ID=$1 | |
OUT_FILENAME=$2 | |
if [ "${GIST_ID}" = "" ]; | |
then | |
echo "ERROR: Gist id not provided" | |
gist_usage | |
return 1 | |
fi | |
if [ "${OUT_FILENAME}" = "" ]; | |
then | |
echo "ERROR: Output filename not provided" | |
gist_usage | |
return 1 | |
fi | |
if [ "${VERBOSE}" -eq 1 ]; | |
then | |
echo "Using token: ${GITHUB_TOKEN}" | |
echo "URL: ${GIST_DOWNLOAD_URL}/${GITHUB_USER}/${GIST_ID}/raw" | |
fi | |
curl -X GET -L --user ${GITHUB_TOKEN} ${GIST_DOWNLOAD_URL}/${GITHUB_USER}/${GIST_ID}/raw -o ${OUT_FILENAME} | |
} | |
function gist_upload(){ | |
FNAME=$1 | |
FDESC=$2 | |
if [ "${FDESC}" = "" ]; | |
then | |
echo "Uploading gist [${FNAME}]..." | |
quickgist ${FNAME} | |
else | |
echo "Uploading gist [${FNAME}] with description [${FDESC}]..." | |
quickgist -d "${FDESC}" ${FNAME} | |
fi | |
} | |
function gist(){ | |
#validate and process the arguments passed | |
if [ "$1" != "" ]; | |
then | |
#if help option passed | |
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; | |
then | |
gist_usage | |
return 0 | |
elif [ "$1" = "-d" ] || [ "$1" = "--download" ]; | |
then | |
# call download func $2=gist id and $3=out filename | |
gist_download $2 $3 | |
return 0 | |
elif [ "$1" = "-l" ] || [ "$1" = "--list" ]; | |
then | |
#list all gists | |
gist_list | |
return 0 | |
else | |
gist_upload $1 $2 | |
return 0 | |
fi | |
else | |
#In case of no args, show usage help | |
echo "Please provide filename to be processed" | |
gist_usage | |
return 1 | |
fi | |
} | |
function commit(){ | |
dt=`date` | |
git add . | |
git commit -m "[$dt] - $1" | |
git push origin master | |
} | |
function pull_from_github(){ | |
git fetch --all | |
git reset --hard FETCH_HEAD | |
git clean -df | |
echo "Project refreshed from repo" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment