-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.py
56 lines (50 loc) · 2.02 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import logging
import sys
import click
from cratedb_toolkit.cmd.tail.main import TableTailer
from cratedb_toolkit.model import TableAddress
from cratedb_toolkit.util import DatabaseAdapter
from cratedb_toolkit.util.cli import boot_click
logger = logging.getLogger(__name__)
cratedb_sqlalchemy_option = click.option(
"--cratedb-sqlalchemy-url", envvar="CRATEDB_SQLALCHEMY_URL", type=str, required=False, help="CrateDB SQLAlchemy URL"
)
@click.command()
@cratedb_sqlalchemy_option
@click.option(
"--lines", "-n", type=int, required=False, default=10, help="Displays n last lines of the input. Default: 10"
)
@click.option("--format", "format_", type=str, required=False, help="Select output format. Default: log / jsonl")
@click.option("--follow", "-f", is_flag=True, required=False, help="Follow new records added, by polling the table")
@click.option(
"--interval", "-i", type=float, required=False, help="When following the tail, poll each N seconds. Default: 0.5"
)
@click.option("--verbose", is_flag=True, required=False, help="Turn on logging")
@click.option("--debug", is_flag=True, required=False, help="Turn on logging with debug level")
@click.argument("resource", nargs=-1, type=click.UNPROCESSED)
@click.version_option()
@click.pass_context
def cli(
ctx: click.Context,
cratedb_sqlalchemy_url: str,
resource: str,
lines: int,
format_: str,
follow: bool,
interval: float,
verbose: bool,
debug: bool,
):
"""
A polling tail implementation for database tables.
"""
if not cratedb_sqlalchemy_url:
logger.error("Unable to operate without database address")
sys.exit(1)
boot_click(ctx, verbose, debug)
adapter = DatabaseAdapter(dburi=cratedb_sqlalchemy_url)
# TODO: Tail multiple tables.
if len(resource) > 1:
raise NotImplementedError("`ctk tail` currently implements tailing a single table only")
tt = TableTailer(db=adapter, resource=TableAddress.from_string(resource[0]), interval=interval, format=format_)
tt.start(lines=lines, follow=follow)