2014-10-12 21:31:54 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from __future__ import unicode_literals
|
2014-11-21 10:15:50 +03:00
|
|
|
from __future__ import print_function
|
2014-10-13 02:07:34 +04:00
|
|
|
|
|
|
|
import click
|
2014-10-12 21:31:54 +04:00
|
|
|
|
2014-10-12 21:45:35 +04:00
|
|
|
from prompt_toolkit import CommandLineInterface, AbortAction, Exit
|
2014-11-21 10:15:50 +03:00
|
|
|
from pgcompleter import PGCompleter
|
|
|
|
from pgstyle import PGStyle
|
2014-11-22 10:43:11 +03:00
|
|
|
from pgexecute import PGExecute
|
2014-10-12 21:31:54 +04:00
|
|
|
from prompt_toolkit.line import Line
|
|
|
|
from prompt_toolkit.layout import Layout
|
|
|
|
from prompt_toolkit.layout.prompt import DefaultPrompt
|
2014-11-21 10:15:50 +03:00
|
|
|
from prompt_toolkit.layout.menus import CompletionsMenu
|
2014-10-12 21:31:54 +04:00
|
|
|
|
2014-11-21 10:15:50 +03:00
|
|
|
from pygments.lexers import SqlLexer
|
2014-10-12 21:31:54 +04:00
|
|
|
|
2014-10-13 02:07:34 +04:00
|
|
|
@click.command()
|
2014-11-21 10:15:50 +03:00
|
|
|
@click.option('-h', '--host', default='localhost')
|
2014-11-22 10:43:11 +03:00
|
|
|
@click.option('-p', '--port', default=5432)
|
2014-11-21 10:15:50 +03:00
|
|
|
@click.option('-U', '--user', prompt=True, envvar='USER')
|
2014-11-23 11:09:00 +03:00
|
|
|
@click.password_option('-W', '--password', default='',
|
2014-11-21 10:15:50 +03:00
|
|
|
confirmation_prompt=False)
|
|
|
|
@click.argument('database', envvar='USER')
|
2014-11-22 10:43:11 +03:00
|
|
|
def pgcli(database, user, password, host, port):
|
2014-11-23 11:09:00 +03:00
|
|
|
try:
|
|
|
|
pgexecute = PGExecute(database, user, password, host, port)
|
|
|
|
except Exception as e:
|
|
|
|
click.secho(e.message, err=True, fg='red')
|
|
|
|
exit(1)
|
2014-11-22 10:43:11 +03:00
|
|
|
layout = Layout(before_input=DefaultPrompt('%s> ' % database),
|
2014-11-21 10:15:50 +03:00
|
|
|
menus=[CompletionsMenu()],
|
2014-10-12 21:45:35 +04:00
|
|
|
lexer=SqlLexer)
|
2014-11-21 10:15:50 +03:00
|
|
|
line = Line(completer=PGCompleter())
|
|
|
|
cli = CommandLineInterface(style=PGStyle, layout=layout, line=line)
|
2014-10-12 21:31:54 +04:00
|
|
|
|
2014-10-12 21:45:35 +04:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
document = cli.read_input(on_exit=AbortAction.RAISE_EXCEPTION)
|
2014-11-23 11:09:00 +03:00
|
|
|
try:
|
|
|
|
print(pgexecute.run(document.text))
|
|
|
|
except Exception as e:
|
|
|
|
click.secho("Does not compute!", fg='red')
|
|
|
|
click.secho(e.message, err=True, fg='red')
|
2014-10-12 21:45:35 +04:00
|
|
|
except Exit:
|
2014-11-21 10:15:50 +03:00
|
|
|
print ('GoodBye!')
|