Created
August 26, 2014 03:06
-
-
Save deontologician/e88141d949ac63fe0eef to your computer and use it in GitHub Desktop.
Script for running RethinkDB ReQL queries from the commandline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a full-blown package of this here: http://github.com/deontologician/reql_cli