Last active
December 7, 2016 21:28
-
-
Save pcapriotti/50f00944c6ff8615a849d54905fcb13e to your computer and use it in GitHub Desktop.
Script to create a new gist
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
#!/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