Skip to content

Instantly share code, notes, and snippets.

@cverbiest
Last active May 2, 2020 19:02
Show Gist options
  • Save cverbiest/9bd3b47c5c3195f5f613 to your computer and use it in GitHub Desktop.
Save cverbiest/9bd3b47c5c3195f5f613 to your computer and use it in GitHub Desktop.
gistpost
#!/bin/bash
# Purpose : post a file as github gist
# Author : Carl Verbiest
typeset local
script=$0
script_dir=${script%/*}
script=${script##*/}
[ -f ~/.ssh/github.env ] && . ~/.ssh/github.env
TEE=""
PUBLIC=false
Usage() {
cat <<END
Usage $0 [-u user] [-p password] [-w] file ...
-u : username, default \$GITHUB_USER
-p : password, default \$GITHUB_PASSWORD
-w : show it to the world = public gist
-d : debug
END
exit 1
}
postfile() {
ff=$1
fn=${ff##*/}
cat <<END
{
"description":"$fn",
"public":$PUBLIC,
"files": {
"$fn": {
"content":"$(sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/$/\\n/' <$ff)"
}
}
}
END
}
set -- `getopt u:p:wd $*`
if [ $? != 0 ]
then
Usage
fi
for i in $*
do
case $i in
-u) GITHUB_USER=$2; shift; shift;;
-p) GITHUB_PASSWORD=$2; shift; shift;;
-w) PUBLIC=true; shift;;
-d) TEE="| tee"; shift;;
--) shift; break;;
esac
done
[ ! -f "$1" ] && Usage
for file in $*
do
echo Posting $file
postfile $file $TEE | curl --user "$GITHUB_USER:$GITHUB_PASSWORD" --data @- https://api.github.com/gists
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment