Skip to content

Instantly share code, notes, and snippets.

@snowch
Last active August 29, 2015 14:08
Show Gist options
  • Save snowch/72618dda6d6ad8a80d97 to your computer and use it in GitHub Desktop.
Save snowch/72618dda6d6ad8a80d97 to your computer and use it in GitHub Desktop.
cloudant query utility script
#!/bin/bash
set -e
USERNAME=snowch
DBNAME=stackoverflow
SCRIPT=$(basename $0)
if [[ ! $1 =~ (create)|(list)|(delete)|(find) ]]; then
echo "Usage: ./$SCRIPT create|list|delete|find"
exit 1
fi
if [[ ! -e ~/.netrc ]]; then
echo "Could not find your ~/.netrc file containing your Cloudant login credentials. For example:"
echo ""
echo "machine <<username>>.cloudant.com login <<username>> password <<password>>"
exit 1
fi
if [[ $1 == 'find' ]];
then
curl -n -X POST https://$USERNAME.cloudant.com/$DBNAME/_find -d '
{
"selector": {
"subject": {"$eq": "SAMPLE TOPIC"}
},
"fields": ["_id", "_rev", "subject"],
"sort": [{"subject": "asc"}],
"limit": 10,
"skip": 0
}'
fi
if [[ $1 == 'create' ]];
then
curl -n -X POST https://$USERNAME.cloudant.com/$DBNAME/_index -d '
{
"index": {
"fields": ["subject"]
},
"name" : "subject",
"type" : "json"
}'
fi
if [[ $1 == 'list' ]];
then
curl -n -s -X GET https://$USERNAME.cloudant.com/$DBNAME/_index | python -m json.tool
fi
if [[ $1 == 'delete' ]];
then
if [[ -z $2 ]]; then
echo "Usage: ./$SCRIPT delete design_doc/type/name"
exit 1
fi
curl -n -s -X DELETE https://$USERNAME.cloudant.com/$DBNAME/_index/$2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment