Jaymon / prom

A PostgreSQL or SQLite orm for Python
MIT License
22 stars 4 forks source link

prom base cli command #41

Open Jaymon opened 7 years ago

Jaymon commented 7 years ago

It would be cool to have a simple prom command:

$ prom

That would just connect to whatever shell the database your PROM_DSN points to without you having to do anything else, I think we would need to use pexpect though to turn control from the python script to the db shell (eg, psql)

import pexpect
child = pexpect.spawn ('psql ...')
child.interact()     # Give control of the child to the user.
@arg("--connection-name", "-c",
    dest="conn_name",
    default="",
    help="the connection name (from prom dsn) you want to restore")
def main(conn_name):
    """quick way to get into the db shell using prom dsn"""
    inter = get_interface(conn_name)
    conn = inter.connection_config

    if "postgres" in conn.interface_name.lower():
        # TODO -- need to write password to pgpass file and set environment variable
        # and call psql, I don't think you can put the password on the cli
        cmd = [
            "psql",
            "--dbname",
            conn.database,
            "--username",
            conn.username,
            "--password",
            conn.password,
            "--host",
            conn.host,
            "--port",
            str(conn.port),
        ]

    elif "sqlite" in conn.interface_name.lower():
        cmd = [
            "sqlite3",
            conn.database
        ]
    else:
        raise RuntimeError("Unsupported interface")

    # pexpect code goes here