Skip to content

Instantly share code, notes, and snippets.

@deontologician
Created August 26, 2014 03:06
Show Gist options
  • Save deontologician/e88141d949ac63fe0eef to your computer and use it in GitHub Desktop.
Save deontologician/e88141d949ac63fe0eef to your computer and use it in GitHub Desktop.
Script for running RethinkDB ReQL queries from the commandline
#!/usr/bin/env python2
'''Quick and dirty script for executing reql from the commandline'''
import json
import argparse
import rethinkdb as r
def main(argv):
parser = argparse.ArgumentParser(description='Run ReQL commands')
parser.add_argument('--port', metavar='PORT', type=int, default=28015,
help='RethinkDB driver port')
parser.add_argument('--host', metavar='HOST', default='localhost',
help='RethinkDB host address')
parser.add_argument('--db', metavar='DB', default='test',
help='default db for queries')
parser.add_argument('QUERY', help='ReQL query to run')
args = parser.parse_args()
conn = r.connect(host=args.host, port=args.port, db=args.db)
query = r.expr(eval(args.QUERY, {
'r': r,
'__builtins__': {
'True': True,
'False': False,
'None': None,
}
}))
try:
result = query.run(conn)
if isinstance(result, dict) and 'first_error' in result:
print result['first_error'].replace('\t', ' ')
else:
print json.dumps(list(result), indent=True, sort_keys=True)
except r.RqlError as e:
print e
if __name__ == '__main__':
import sys
main(sys.argv)
@deontologician
Copy link
Author

There's a full-blown package of this here: http://github.com/deontologician/reql_cli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment