CouchDB command-line tool to allow shortened curl commands without putting username/password in your command-line history
If you use CouchDB, then you can access everything using curl. The trouble is that it if you are using an authenticated, hosted service such as Cloudant's, then your credentials appear on your command-line history and there is a lot of typing. e.g.
curl 'https://mypassword:[email protected]/database/12345678'
With ccurl, this becomes:
ccurl /database/12345678
Or adding a document with curl:
curl -X POST -H 'Content-type:application/json' -d'{"a":1,"b":2}' 'https://mypassword:[email protected]/database'
With ccurl, this becomes:
ccurl -X POST -d'{"a":1,"b":2}' /database
ccurl requires Node.js (and npm). Simply type:
npm install -g ccurl
Your CouchDB credentials are taken from an environment variable "COUCH_URL". This can be set in your console with
export COUCH_URL="https://mypassword:[email protected]"
or this line can be added to your "/.bashrc" or "/.bash_profile" file.
If you don't want credentials stored in your command-line history, you can set an environment variable by extracting the credentials from a file e.g.
export COUCH_URL=`cat ~/.ibm/cloudant.json | jq -r .url`
where ~/.ibm/cloudant.json
is a JSON file that is readable only by my user containing the Cloudant service credentials.
If you prefer to use IBM's IAM authentication for a Cloudant service set up two environment variables:
COUCH_URL
- the URL of your Cloudant service e.g. https://myurl.cloudant.com
(note the absence of authentication credentials).IAM_API_KEY
- the IBM IAM API key that identifies you.ccurl
exchanges your API key for a "bearer token" which is automatically inserted into the request. ccurl
keeps a cache of the bearer token for subsequent requests. It's stored in ~/.ccurl
.
> ccurl -X PUT /newdatabase
{"ok":true}
> ccurl -X POST -d'{"a":1,"b":2}' /newdatabase
{"ok":true,"id":"005fa466b4f690ccad7b4d194f071bbe","rev":"1-25f9b97d75a648d1fcd23f0a73d2776e"}
> ccurl /newdatabase/005fa466b4f690ccad7b4d194f071bbe
{"_id":"005fa466b4f690ccad7b4d194f071bbe","_rev":"1-25f9b97d75a648d1fcd23f0a73d2776e","a":1,"b":2}
> ccurl '/newdatabase/_all_docs?limit=10&include_docs=true'
{"total_rows":1,"offset":0,"rows":[{"id":"005fa466b4f690ccad7b4d194f071bbe","key":"005fa466b4f690ccad7b4d194f071bbe","value":{"rev":"1-25f9b97d75a648d1fcd23f0a73d2776e"},"doc":{"_id":"005fa466b4f690ccad7b4d194f071bbe","_rev":"1-25f9b97d75a648d1fcd23f0a73d2776e","a":1,"b":2}}]}
> ccurl -X DELETE /newdatabase
{"ok":true}
ccurl -h
ccurl -v
etc.
If jq is installed, ccurl automatically pipes the curl output to jq .
, when stdout is a terminal. You may also do the piping yourself to extract a subset of the rdata e.g
ccurl '/newdatabase/_all_docs?limit=10&include_docs=true' | jq '.total_rows'
or
ccurl '/newdatabase/_all_docs?limit=10&include_docs=true' | jq '.rows[0].doc.name | length'