couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jason Huggins" <ja...@jrandolph.com>
Subject Appending "\n" and pretty printing output from the command line using curl (with a smidge of python)
Date Wed, 20 Aug 2008 18:04:49 GMT
Hello all!

Briefly, introducing myself... and please forgive the blatant
self-promotion -- I'm recently former Googler, now working on my own
startup in Chicago (with some others still in San Francisco). Django
and CouchDB weigh heavily in our server setup and future API...  I'm
*loving* CouchDB... And I've pretty much devoured every online
presentation or tech talk video that exists on CouchDB so far. :-)
Keep it up!

With that out of the way.... I just wanted to send along a quick tip
for folks using curl. If this is useful, maybe some version of this
should probably graduate to a "GettingStartedWithCurl" wiki page:

I'm frequently annoyed that curl doesn't end the data stream with a
"\n" at the end... so a curl request usually looks like this:

----------------------------
ubuntu: ~ hugs$ curl -u http://mycouchdb
{"couchdb":"Welcome","version":"0.8.0-incubating"}ubuntu: ~ hugs$
----------------------------

I was thinking about creating a patch to add a "\n" at the end of
every request, but I figured that would be a request of last resort.

The stupid quick solution is append an empty "echo" command like so:
--------------------------------------------------------
ubuntu: ~ hugs$ curl http://mycouchdb ; echo ''
{"couchdb":"Welcome","version":"0.8.0-incubating"}
ubuntu: ~ hugs$
--------------------------------------------------------

I could have stopped there... but the urge to bikeshed this further
was just too great... So, I whipped up a quick python script that I
now pipe to to do my "post-processing". I remembered that the
GettingStartedWithPython wiki page simply does a pretty print of the
content... So I heavily streamlined that script to make it play nicer
with curl. So this is the result now:

--------------------------------------------------------
ubuntu: ~ hugs$ curl -s http://mycouchdb | ./pprint-json
{
    "couchdb": "Welcome",
    "version": "0.8.0-incubating"
}
ubuntu: ~ hugs$
--------------------------------------------------------


.... Ah... much better. :-)


Here's the script:
--------------------------------------------------------
#! /usr/bin/python

import sys
import simplejson

raw_input = sys.stdin.read()
json_data = simplejson.loads(raw_input)

# Make pretty!
print simplejson.dumps(json_data, sort_keys=True, indent=4)
--------------------------------------------------------

To get this to work correctly, I also had to add the silent "-s" flag
to curl so that it didn't print a progress meter with the results as
well. (Try it without "-s" to see what I mean.)

I would love it there was "pretty print all output" configuration
option for Couch... but until then, I'll just use my script. :-)

Cheers and thanks for all the JSON!

- Jason Huggins

Mime
View raw message