Created
January 13, 2011 23:29
-
-
Save oogali/778838 to your computer and use it in GitHub Desktop.
Shell script interface to corkboard.me
This file contains hidden or 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/sh | |
## corkboard.me shell script interface | |
## -- omachonu ogali / @oogali | |
## | |
## hi tim! | |
## | |
## still a work in progress, i still need to finish the note | |
## positioning stuff. comments appreciated. | |
## -oo | |
## | |
CORKBOARD="http://corkboard.me" | |
ECHO=/bin/echo | |
CURL=`which curl 2>/dev/null` | |
if [ -z "${CURL}" ]; then | |
echo "Please install curl..." | |
exit 1 | |
fi | |
CURL_ARGS="-s -A 'corkboard.me/shellhackery-20101203 (http://twitter.com/oogali)'" | |
usage() { | |
echo | |
echo "corkboard <id> <action> <data>" | |
echo | |
echo "* actions: {new, tack, list, modify, remove, trash}" | |
echo " new: returns a new corkboard id" | |
echo " tack: adds a new note to an existing corkboard" | |
echo " list: returns all notes on a corkboard" | |
echo " modify: sets the text of a corkboard note" | |
echo " remove: adds a new note to an existing corkboard" | |
echo " trash: trashes all existing notes on a corkboard" | |
echo | |
exit 1 | |
} | |
if [ $# -lt 1 ]; then | |
usage | |
fi | |
me=`basename $0` | |
case "$1" in | |
new) | |
cbid=`${CURL} ${CURL_ARGS} -I ${CORKBOARD} | grep '^Location: ' | cut -f4 -d '/'` | |
if [ -z "${cbid}" ]; then | |
echo "${me}: could not create a new corkboard, sorry dude." | |
exit 1 | |
fi | |
echo "${me}: new ID is ${cbid}" | |
;; | |
tack) | |
if [ $# -ne 3 ]; then | |
echo "${me}: tack only takes two arguments: the corkboard id, and a message" | |
exit 1 | |
fi | |
corked=`mktemp /tmp/corkboard.me.XXXXXX` | |
if [ -z "${corked}" ] || [ ! -f "${corked}" ]; then | |
echo "${me}: could not create temporary file on disk" | |
exit 1 | |
fi | |
CURL_ARGS="${CURL_ARGS} -H Content-Type: -d @- -o ${corked}" | |
cat << EOF | ${CURL} ${CURL_ARGS} "${CORKBOARD}/cork/${2}/save" | |
[{"objectType":"postit","id":null,"x":0,"y":0,"z":"1","data":{"content":"${3}","width":200,"height":180}}] | |
EOF | |
grep -q '"success":true' "${corked}" | |
if [ $? -ne 0 ]; then | |
echo "${me}: could not tack new note onto corkboard, lo siento." | |
exit 1 | |
fi | |
nid=`cat "${corked}" | sed 's/.*"id":\([0-9]*\).*/\1/g'` | |
echo "${me}: corkboard ${2}, note ${nid}" | |
rm -f ${corked} | |
;; | |
list) | |
if [ $# -ne 2 ]; then | |
echo "${me}: remove only takes one argument: the corkboard id" | |
exit 1 | |
fi | |
${CURL} ${CURL_ARGS} "${CORKBOARD}/cork/${2}/load" | awk 'BEGIN { RS="{" } { print $0 }' | grep 'content' | sed 's/.*"content":"\(.*\)".*"id":\([0-9]*\).*/\2: \1/g; s/",".*//g' | |
;; | |
modify) | |
if [ $# -ne 4 ]; then | |
echo "${me}: modify only takes three arguments: the corkboard id, the note id, and the message" | |
exit 1 | |
fi | |
CURL_ARGS="${CURL_ARGS} -H Content-Type: -d @-" | |
cat << EOF | ${CURL} ${CURL_ARGS} "${CORKBOARD}/cork/${2}/save" | grep -q '"success":true' | |
[{"objectType":"postit","id":${3},"data":{"content":"${4}"}}] | |
EOF | |
if [ $? -ne 0 ]; then | |
echo "${me}: could not modify existing note on corkboard, perdoname." | |
exit 1 | |
fi | |
echo "${me}: corkboard ${2}, note ${3} modified" | |
;; | |
remove) | |
if [ $# -ne 3 ]; then | |
echo "${me}: remove only takes two arguments: the corkboard id, and a message" | |
exit 1 | |
fi | |
${CURL} ${CURL_ARGS} "${CORKBOARD}/cork/${2}/${3}" | grep -q '"success":true' | |
if [ $? -ne 0 ]; then | |
echo "${me}: could not tack new note onto corkboard, je suis desole." | |
exit 1 | |
fi | |
echo "${me}: corkboard ${2}, removed note ${3}" | |
;; | |
trash) | |
for nid in `${CURL} ${CURL_ARGS} "${CORKBOARD}/cork/${2}/load" | awk 'BEGIN { RS="{" } { print $0 }' | grep 'content' | sed 's/.*"id":\([0-9]*\).*/\1/g'` ; do | |
${ECHO} -n "deleting note ${nid}..." | |
${CURL} ${CURL_ARGS} "${CORKBOARD}/cork/${2}/${nid}" | grep -q '"success":true' | |
if [ $? -ne 0 ]; then | |
echo "failed" | |
else | |
echo "ok" | |
fi | |
done | |
;; | |
*) | |
usage | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment