Skip to content

Instantly share code, notes, and snippets.

@pcapriotti
Last active December 7, 2016 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcapriotti/50f00944c6ff8615a849d54905fcb13e to your computer and use it in GitHub Desktop.
Save pcapriotti/50f00944c6ff8615a849d54905fcb13e to your computer and use it in GitHub Desktop.
Script to create a new gist
#!/bin/bash
set -e
usage() {
echo "Usage: $0 [-f FILE] [-n NAME] [-l LANGUAGE] [-p] [-d] DESCRIPTION" 2>&1
exit 1
}
public=true
dryrun=false
while getopts n:f:l:pd FLAG; do
case $FLAG in
n)
name=$OPTARG
;;
f)
file=$OPTARG
[ -z "$name" ] && name=$file
;;
l)
lang=$OPTARG
;;
p)
public=false
;;
d)
dryrun=true
;;
esac
done
[ -z "$name" ] && usage
[ -z "$file" ] && file=/dev/stdin
shift $((OPTIND-1))
desc="$*"
run() {
if [ "$dryrun" == true ]; then
cat
else
github POST /gists | jq -r '.html_url'
fi
}
<$file jq -Rs --arg desc "$desc" \
--argjson public "$public" \
--arg name "$name" \
--arg lang "$lang" '
{
description: "\($desc)",
public: $public,
files: {
"\($name)" : { content: . }
}
} + (if (($lang | length) > 0) then { language: $lang } else {} end)
' | run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment