Skip to content

Instantly share code, notes, and snippets.

@mgoellnitz
Last active March 28, 2026 12:28
Show Gist options
  • Select an option

  • Save mgoellnitz/103757dea40f51c1c1376580a89a7773 to your computer and use it in GitHub Desktop.

Select an option

Save mgoellnitz/103757dea40f51c1c1376580a89a7773 to your computer and use it in GitHub Desktop.
Gist Command Line Tool for Single File Gists
#!/bin/sh
#
# Copyright 2016-2026 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/>.
#
usage() {
echo "Gist Command Line Tool '$(basename "$0")'" 1>&2
echo "" 1>&2
echo "$0 [-h] [-u user] [-t token] [-p] filename [Title]" 1>&2
echo "" 1>&2
echo "If 'filename' points to an existing file, it is uploaded to GitHub with an optional title." 1>&2
echo "If 'filename' describes a filename present in the user's gists, it is downloaded." 1>&2
echo "If 'filename' is not present, the available snippets of the user are listet." 1>&2
echo "" 1>&2
echo " -h this help screen" 1>&2
echo "" 1>&2
echo " -p make gist public (only available on creation)" 1>&2
echo "" 1>&2
echo " -t github api token - default \$GITHUB_COM_TOKEN" 1>&2
echo "" 1>&2
echo " -u user name - default \$GITHUB_COM_USER" 1>&2
echo "" 1>&2
}
if [ "$(which jq|wc -l)" = 0 ] ; then
echo "To use this tool, jq must be installed." 1>&2
exit 1
fi
# defaults
API="https://api.github.com"
GITHUBUSER=$GITHUB_COM_USER
TOKEN=$GITHUB_COM_TOKEN
PUBLIC=false
while getopts "hpt:u:" opt ; do
case "${opt}" in
h)
usage
exit
;;
p)
PUBLIC="true"
;;
t)
TOKEN=$OPTARG
;;
u)
GITHUBUSER=$OPTARG
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
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 '.[]|.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=$(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" 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