Skip to content

Instantly share code, notes, and snippets.

@mgoellnitz
Last active December 7, 2022 14:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgoellnitz/103757dea40f51c1c1376580a89a7773 to your computer and use it in GitHub Desktop.
Save mgoellnitz/103757dea40f51c1c1376580a89a7773 to your computer and use it in GitHub Desktop.
Gist Command Line Tool for Single File Gists
#!/bin/bash
#
# Copyright 2016-2021 Martin Goellnitz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
if [ $(which jq|wc -l) = 0 ] ; then
echo "To use this tool, jq must be installed."
exit
fi
if [ "$#" == "0" ]; then
echo "Gist Command Line Tool 'gist.sh'"
echo ""
echo "$0 [-u user] [-t token] filename [Title]"
echo ""
echo "If 'filename' points to an existing file, it is uploaded to GitHub with an optional title."
echo "If 'filename' describes a filename present in the user's gists, it is downloaded."
echo "If 'filename' is not present, the available snippets of the user are listet."
echo ""
echo " -u user name - default \$GITHUB_COM_USER"
echo ""
echo " -t github api token - default \$GITHUB_COM_TOKEN"
echo ""
echo " -p make gist public (only available on creation)"
echo ""
exit 1
fi
# defaults
API="https://api.github.com"
GITHUBUSER=$GITHUB_COM_USER
TOKEN=$GITHUB_COM_TOKEN
PUBLIC=false
PSTART=`echo $1|sed -e 's/^\(.\).*/\1/g'`
while [ "$PSTART" = "-" ] ; do
if [ "$1" = "-u" ] ; then
shift
GITHUBUSER="$1"
fi
if [ "$1" = "-t" ] ; then
shift
TOKEN="$1"
fi
if [ "$1" = "-p" ] ; then
PUBLIC="true"
fi
shift
PSTART=`echo $1|sed -e 's/^\(.\).*/\1/g'`
done
FILEPATH=$1
TITLE=$2
if [ -z "$GITHUBUSER" ] ; then
echo "Cannot work without the context of a github user name. Sorry..."
exit 1
fi
if [ -z "$FILEPATH" ] ; then
echo "Listing gists of $GITHUBUSER"
# curl -k -H "Authorization: token $TOKEN" ${API}/users/${GITHUBUSER}/gists 2> /dev/null|jq '.[]'
curl -k -H "Authorization: token $TOKEN" ${API}/users/${GITHUBUSER}/gists 2> /dev/null|jq '.[]|.description,.files[].filename,""'|sed -e s/^\"//g|sed -e s/\"$//g
else
FILE=`basename $FILEPATH`
GID=`curl -k -H "Authorization: token $TOKEN" ${API}/users/${GITHUBUSER}/gists 2> /dev/null \
|jq '.[]|select(.files["'$FILE'"])|.id'|head -1\
|sed -e s/^\"//g|sed -e s/\"$//g`
# echo "gist id: $GID"
if [ -f $FILEPATH ] ; then
# CONTENT=$(cat $FILEPATH|sed -e 's/\"/\\"/g')
CONTENT=$(sed -e 's/\r//' -e 's/\\/\\\\/g' -e's/\t/\\t/g' -e 's/"/\\"/g' -e 's/%/%%/g' "${FILEPATH}" | awk '{ printf($0 "\\n") }')
if [ -z "$GID" ] ; then
if [ -z "$TITLE" ] ; then
TITLE=$FILE
fi
echo "Creating file '$FILE' with title '$TITLE' for user $GITHUBUSER"
DATA="{\"description\": \"${TITLE}\", \"public\": ${PUBLIC}, \"files\": { \"${FILE}\": { \"content\" : \"${CONTENT}\" } } }\""
# echo $DATA
curl -X POST -k -H "Authorization: token $TOKEN" --data "$DATA" ${API}/gists 2> /dev/null > /dev/null
else
if [ -z "$TITLE" ] ; then
echo "Updating file '$FILE' for user $GITHUBUSER"
DATA="{\"files\": { \"${FILE}\": { \"content\" : \"${CONTENT}\" } } }\""
else
echo "Updating file '$FILE' with title '$TITLE' for user $GITHUBUSER"
DATA="{\"description\": \"${TITLE}\", \"files\": { \"${FILE}\": { \"content\" : \"${CONTENT}\" } } }\""
fi
# echo $DATA
# curl -X PATCH -k -H "Authorization: token $TOKEN" --data "$DATA" ${API}/gists/${GID}
curl -X PATCH -k -H "Authorization: token $TOKEN" --data "$DATA" ${API}/gists/${GID} 2> /dev/null > /dev/null
fi
else
DL=`curl -k -H "Authorization: token $TOKEN" ${API}/users/${GITHUBUSER}/gists 2> /dev/null \
|jq '.[]|select(.files["'$FILE'"])|.files["'$FILE'"].raw_url'|head -1\
|sed -e s/^\"//g|sed -e s/\"$//g`
# echo "$GID: $DL"
if [ ! -z "$DL" ] ; then
echo "Fetching file '$FILE' from $GITHUBUSER as '$FILEPATH'"
curl -k -H "Authorization: token $TOKEN" ${DL} 2> /dev/null > $FILEPATH
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment