Skip to content

Commit 60f5faf

Browse files
committed
Add 'ping' command and rework to use 'main' command group
1 parent 8e1ca49 commit 60f5faf

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

embeddings/cli.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
2+
import time
23
from datetime import timedelta
3-
from time import perf_counter
44

55
import click
66

@@ -9,20 +9,43 @@
99
logger = logging.getLogger(__name__)
1010

1111

12-
@click.command()
12+
@click.group("embeddings")
1313
@click.option(
14-
"-v", "--verbose", is_flag=True, help="Pass to log at debug level instead of info"
14+
"-v",
15+
"--verbose",
16+
is_flag=True,
17+
help="Pass to log at debug level instead of info",
1518
)
16-
def main(*, verbose: bool) -> None:
17-
start_time = perf_counter()
19+
@click.pass_context
20+
def main(
21+
ctx: click.Context,
22+
*,
23+
verbose: bool,
24+
) -> None:
25+
ctx.ensure_object(dict)
26+
ctx.obj["start_time"] = time.perf_counter()
27+
1828
root_logger = logging.getLogger()
1929
logger.info(configure_logger(root_logger, verbose=verbose))
2030
logger.info(configure_sentry())
2131
logger.info("Running process")
2232

23-
# Do things here!
33+
def _log_command_elapsed_time() -> None:
34+
elapsed_time = time.perf_counter() - ctx.obj["start_time"]
35+
logger.info(
36+
"Total time to complete process: %s", str(timedelta(seconds=elapsed_time))
37+
)
38+
39+
ctx.call_on_close(_log_command_elapsed_time)
40+
41+
42+
@main.command()
43+
def ping() -> None:
44+
"""Emit 'pong' to debug logs and stdout."""
45+
logger.debug("pong")
46+
click.echo("pong")
47+
2448

25-
elapsed_time = perf_counter() - start_time
26-
logger.info(
27-
"Total time to complete process: %s", str(timedelta(seconds=elapsed_time))
28-
)
49+
if __name__ == "__main__": # pragma: no cover
50+
logger = logging.getLogger("embeddings.main")
51+
main()

tests/test_cli.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
from embeddings.cli import main
22

33

4-
def test_cli_no_options(caplog, runner):
5-
result = runner.invoke(main)
4+
def test_cli_default_logging(caplog, runner):
5+
result = runner.invoke(main, ["ping"])
66
assert result.exit_code == 0
77
assert "Logger 'root' configured with level=INFO" in caplog.text
8-
assert "Running process" in caplog.text
9-
assert "Total time to complete process" in caplog.text
108

119

12-
def test_cli_all_options(caplog, runner):
13-
result = runner.invoke(main, ["--verbose"])
10+
def test_cli_debug_logging(caplog, runner):
11+
with caplog.at_level("DEBUG"):
12+
result = runner.invoke(main, ["--verbose", "ping"])
1413
assert result.exit_code == 0
1514
assert "Logger 'root' configured with level=DEBUG" in caplog.text
16-
assert "Running process" in caplog.text
17-
assert "Total time to complete process" in caplog.text
15+
assert "pong" in caplog.text
16+
assert "pong" in result.output

0 commit comments

Comments
 (0)