Skip to content

Instantly share code, notes, and snippets.

@micalevisk
Last active February 1, 2018 16:44
Show Gist options
  • Save micalevisk/c923ee2b0cd2f39573e1b1f5fbb85688 to your computer and use it in GitHub Desktop.
Save micalevisk/c923ee2b0cd2f39573e1b1f5fbb85688 to your computer and use it in GitHub Desktop.
GitHub API gist tasks CRUD
#!/bin/bash
#
# Delete a gist of http://gist.github.com/
#
# USE:
# ./deleteGist.sh <github-username> <gist-id>
#
# Created by Micael Levi on 11/26/2016
# Copyright (c) 2016 mllc@icomp.ufam.edu.br; All rights reserved.
#
[ $# -ne 2 ] && { grep -m1 -A1 -w "USE:" $(basename $0); exit 1 ; }
gitusername="${1}"
GIST_ID="${2}"
read -p "Are you sure? (y): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]];then
curl --user "${gitusername}" --request DELETE "https://api.github.com/gists/${GIST_ID}"
[ $? -eq 0 ] && echo "success!"
fi
#!/bin/bash
#
# Edit a gist of http://gist.github.com/ with the gist id
#
# USE:
# ./editGist.sh [-h, --help] [-u, --user="github-username"] <-i, --id="gist-id"> "<oldfilename>[:<newfilename>]" "[new file content]"
#
# Created by Micael Levi on 11/26/2016
# Copyright (c) 2016 mllc@icomp.ufam.edu.br; All rights reserved.
#
OPTS=`getopt -o :hu:i: --long help,user:,id: -n 'parse-options' -- "$@"`
[[ $? != 0 ]] && { echo "Failed parsing options." >&2 ; exit 1 ; }
#echo "[$OPTS][]"
#eval set -- "$OPTS"
while :
do
case "$1" in
-h | --help ) grep -m1 -A1 -w "USE:" $(basename $0); exit 2 ;;
-u | --user ) gitusername="$2"; shift ;;
-i | --id ) gistid="$2"; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
shift
done
#############################################
[[ "$gistid" ]] || exit
DELIM=:
gitusername="${gitusername:-$USERNAME}"
#NEW_GIST_DESCRIPTION="\"description\": \"${NEW_GIST_DESCRIPTION}\","
currentFile="$1"
FILENAME=$(sed -r "s/([^${DELIM}]+)[[:blank:]]*${DELIM}[[:blank:]]*.+/\1/" <<< "$currentFile")
[[ ! $currentFile =~ : ]] || {
NEW_FILENAME=$(sed -r "s/[^${DELIM}]+[[:blank:]]*${DELIM}[[:blank:]]*(.+)/\1/" <<< "$currentFile")
NEW_FILENAME="\"filename\": \"${NEW_FILENAME}\","
}
NEW_FILE_CONTENT="${2:+\"content\": \"$2\"}"
#############################################
jsondata=$(cat <<EOF
{
${NEW_GIST_DESCRIPTION}
"files": {
"${FILENAME}": {
${NEW_FILENAME}
${NEW_FILE_CONTENT}
}
}
}
EOF
)
echo -e "$jsondata\n"
read -p "Edit gist '${gistid}' of '${gitusername}' (y)? " -rn1
echo
[[ $REPLY =~ ^[Yy]$ ]] || exit 3
curl -s \
--user "${gitusername}" \
--request PATCH \
--data "$jsondata" "https://api.github.com/gists/${gistid}" | # --data @myfile.json
grep -m1 -Po '(?<="git_pull_url": ")[^,]+(?=",)' # show gist url
# JSON MINIFIER: http://www.httputility.net/json-minifier.aspx
#!/bin/bash
#
# Create a public (or secret) gist on http://gist.github.com/
# and returns the link to gist created (if successful).
#
# USE:
# ./postGist.sh [-h, --help] [-u, --user="github-username"] [-s,--secret | -p, --public] "<filename>" "<file description>" "<file content>"
#
# Created by Micael Levi on 11/26/2016
# Copyright (c) 2016 mllc@icomp.ufam.edu.br; All rights reserved.
#
OPTS=`getopt -o :shu: --long secret,help,user: -n 'parse-options' -- "$@"`
[[ $? != 0 ]] && { echo "Failed parsing options." >&2 ; exit 1 ; }
#echo "[$OPTS][]"
#eval set -- "$OPTS"
while :
do
case "$1" in
-s | --secret ) ISPUBLIC="false" ;;
-h | --help ) grep -m1 -A1 -w "USE:" $(basename $0); exit 2 ;;
-u | --user ) gitusername="$2"; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
shift
done
#############################################
gitusername="${gitusername:-$USERNAME}"
ISPUBLIC="${ISPUBLIC:-true}"
FILENAME="${1:-myfile}"
FILE_DESCRIPTION="${2:-Create with postGist}"
FILE_CONTENT="${3:-by micalevisk}"
#############################################
jsondata=$(cat <<EOF
{
"description": "${FILE_DESCRIPTION}",
"public": "${ISPUBLIC}",
"files": {
"${FILENAME}": {
"content": "${FILE_CONTENT}"
}
}
EOF
)
echo -e "$jsondata\n"
read -p "Post gist for '${gitusername}' (y)? " -rn1
echo
[[ $REPLY =~ ^[Yy]$ ]] || exit 3
curl -s \
--user "${gitusername}" \
--request POST \
--data "$jsondata" https://api.github.com/gists | # --data @myfile.json
grep -m1 -Po '(?<="git_pull_url": ")[^,]+(?=",)' # show gist url
# JSON MINIFIER: http://www.httputility.net/json-minifier.aspx
@micalevisk
Copy link
Author

micalevisk commented Dec 5, 2016

Fazer o mesmo para o dpaste.de

  • ler API para criação com o cUrl
  • definir parâmetros (modo de leitura dos dados)

dpaste repository
dpaste API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment