An occasional outlet for my thoughts on life, technology, motorcycles, backpacking, kayaking, skydiving...

Thursday, November 20, 2008

Command line tool for dpaste

There are lots of snippet hosts sites that are good for sharing in chats. I like dpaste because its interface is simple and effective even if it doesn't have an official API. I wrote this tool to help use it from the command line. Here is an easy way for you to get it:

cd ~/bin #assuming you have a personal bin folder in your $PATH
curl -o dpaste http://dpaste.com/92000/plain/
chmod +s dpaste

If you look at the title for that dpaste "paste" you'll see an example of how to use it:
dpaste command line tool example title
There are a lot of really cool things happening there.

  • The tool published itself

  • It used my OS username

  • It captured the entire command line for the title

  • The command included a Unix pipe

  • The command included process substitution



And to top it all off I landed a really cool, rounded paste number (92000).

Just in case that paste disappears I will also place a copy of the code here:

# In order for the default title to capture the complete command, a shebang (#!) must not be used
usage(){
cat < < EOF
dpaste - a command line tool for posting to dpaste.com
Usage:
dpaste [-h] [-l language] [-t title|-u] [-p poster|-a] [file]

Summary:
dpaste accepts a stdin pipe and posts the content to http://dpaste.com
with the command (complete with pipes) as the title and your username as
the name. If you pass a file it will be sent instead of stdin and the
filename will be used as the title. The following flags are available:
-h
-H Hold until nobody has looked at it for 180 days.
-l language
Specify: Python|PythonConsole|Sql|DjangoTemplate|JScript|
Css|Xml|Diff|Ruby|Rhtml|Haskell|Apache|Bash
-t title
Override the default title.
-u Paste untitled.
-p poster
Override the default Name/Email.
-a Paste anonymously.
-- Denote the end of flags. Needed if filename starts with a "-".

Acknowledgments:
Copyright (c) 2008 Richard Bronosky
Offered under the terms of the MIT License.
http://www.opensource.org/licenses/mit-license.php
Created while employed by Atlanta Journal-Constitution
EOF
}

title=$(history|tail -n1|tr "\t" "#"|sed "s/[^#]*#//")
poster=$(id -un);

while [[ $1 == -* ]]; do
case "$1" in
-h|--help|-\?) usage; exit 0;;
-H) hold="-F hold=on"; shift;;
-l) language=$2; shift 2;;
-t) title=$2; shift 2;;
-u) unset title; shift;;
-p) poster=$2; shift 2;;
-a) unset poster; shift;;
--) shift; break;;
esac
done

if [[ -n "$1" ]]; then
content=$1
title=$1
else
content=/dev/stdin
fi

url=$(curl -s -L -o /dev/null -w "%{url_effective}" \
-F "content=<$content" \
-F "language=$language" \
-F "title=$title" \
-F "poster=$poster" \
$hold http://dpaste.com
)

# copy the url to the clipboard if you are using a Mac
$(which pbcopy > /dev/null 2>1) && echo -n "$url" | pbcopy
echo "$url"

# vim:ft=sh:tw=78

No comments:

Post a Comment

Followers

About Me

My photo

Believe it or not, I've never been to jail. Not even for a night. I quit going to bars by the time my fake ID was unnecessary, and I quit hacking into businesses and governments once they started paying me.

Now I just provide for my family and try not to notice politics. I get too angry when I watch how much of my income is taken from me, how it is used, and who is doing it. For the same reason, I can't bear to acknowledge celebrities.

After high school, I went to U of K but the outdated computer engineering curriculum and the seemingly endless amounts of money to be made with my internet specific skills drew me to larger tech markets. I have worked in Silicon Valley, New York City, and have settled in Atlanta.