|
| 1 | +import sys |
1 | 2 | from typing import Optional |
2 | 3 |
|
3 | | -import typer |
4 | | -from rich import print |
5 | | - |
6 | | -from taskbadger import __version__ |
7 | | -from taskbadger.cli import create, get, list_tasks_command, run, update |
8 | | -from taskbadger.config import get_config, write_config |
9 | | -from taskbadger.sdk import _parse_token |
10 | | - |
11 | | -app = typer.Typer( |
12 | | - rich_markup_mode="rich", |
13 | | - context_settings={"help_option_names": ["-h", "--help"]}, |
14 | | -) |
15 | | - |
16 | | - |
17 | | -app.command(context_settings={"allow_extra_args": True, "ignore_unknown_options": False})(run) |
18 | | -app.command(context_settings={"ignore_unknown_options": False})(get) |
19 | | -app.command(context_settings={"ignore_unknown_options": False})(create) |
20 | | -app.command(context_settings={"ignore_unknown_options": False})(update) |
21 | | -app.command("list", context_settings={"ignore_unknown_options": False})(list_tasks_command) |
22 | | - |
23 | | - |
24 | | -def version_callback(value: bool): |
25 | | - if value: |
26 | | - print(f"Task Badger CLI Version: {__version__}") |
27 | | - raise typer.Exit() |
28 | | - |
29 | | - |
30 | | -@app.command() |
31 | | -def configure(ctx: typer.Context): |
32 | | - """Update CLI configuration.""" |
33 | | - config = ctx.meta["tb_config"] |
34 | | - token = typer.prompt("API Key", default=config.token) |
35 | | - parsed = _parse_token(token) |
36 | | - if parsed: |
37 | | - org_slug, project_slug, api_key = parsed |
38 | | - print(f"Project key detected — organization: [green]{org_slug}[/green], project: [green]{project_slug}[/green]") |
39 | | - config.organization_slug = org_slug |
40 | | - config.project_slug = project_slug |
41 | | - config.token = token |
42 | | - else: |
43 | | - config.organization_slug = typer.prompt("Organization slug", default=config.organization_slug) |
44 | | - config.project_slug = typer.prompt("Project slug", default=config.project_slug) |
45 | | - config.token = token |
46 | | - path = write_config(config) |
47 | | - print(f"Config written to [green]{path}[/green]") |
48 | | - |
49 | | - |
50 | | -@app.command() |
51 | | -def docs(): |
52 | | - """Open Task Badger docs in a browser.""" |
53 | | - typer.launch("https://docs.taskbadger.net") |
54 | | - |
55 | | - |
56 | | -@app.command() |
57 | | -def info(ctx: typer.Context): |
58 | | - """Show CLI configuration.""" |
59 | | - config = ctx.meta["tb_config"] |
60 | | - print(str(config)) |
61 | | - |
62 | | - |
63 | | -@app.callback() |
64 | | -def main( |
65 | | - ctx: typer.Context, |
66 | | - org: Optional[str] = typer.Option( |
67 | | - None, |
68 | | - "--org", |
69 | | - "-o", |
70 | | - metavar="TASKBADGER_ORG", |
71 | | - show_default=False, |
72 | | - help="Organization Slug. This will override values from the config file and environment variables.", |
73 | | - ), |
74 | | - project: Optional[str] = typer.Option( |
75 | | - None, |
76 | | - "--project", |
77 | | - "-p", |
78 | | - show_envvar=False, |
79 | | - metavar="TASKBADGER_PROJECT", |
80 | | - show_default=False, |
81 | | - help="Project Slug. This will override values from the config file and environment variables.", |
82 | | - ), |
83 | | - version: Optional[bool] = typer.Option( # noqa |
84 | | - None, |
85 | | - "--version", |
86 | | - callback=version_callback, |
87 | | - is_eager=True, |
88 | | - help="Show CLI Version", |
89 | | - ), |
90 | | -): |
91 | | - """ |
92 | | - Task Badger CLI |
93 | | - """ |
94 | | - config = get_config(org=org, project=project) |
95 | | - ctx.meta["tb_config"] = config |
96 | | - |
97 | | - |
98 | | -if __name__ == "__main__": |
99 | | - app() |
| 4 | +try: |
| 5 | + import typer |
| 6 | + from rich import print |
| 7 | +except ImportError as exc: |
| 8 | + _missing = exc.name or "typer" |
| 9 | + |
| 10 | + def app() -> None: |
| 11 | + sys.stderr.write( |
| 12 | + f"The Task Badger CLI requires the '{_missing}' package, which is not installed.\n" |
| 13 | + "Install the CLI extras with:\n\n" |
| 14 | + " pip install 'taskbadger[cli]'\n" |
| 15 | + ) |
| 16 | + sys.exit(1) |
| 17 | +else: |
| 18 | + from taskbadger import __version__ |
| 19 | + from taskbadger.cli import create, get, list_tasks_command, run, update |
| 20 | + from taskbadger.config import get_config, write_config |
| 21 | + from taskbadger.sdk import _parse_token |
| 22 | + |
| 23 | + app = typer.Typer( |
| 24 | + rich_markup_mode="rich", |
| 25 | + context_settings={"help_option_names": ["-h", "--help"]}, |
| 26 | + ) |
| 27 | + |
| 28 | + app.command(context_settings={"allow_extra_args": True, "ignore_unknown_options": False})(run) |
| 29 | + app.command(context_settings={"ignore_unknown_options": False})(get) |
| 30 | + app.command(context_settings={"ignore_unknown_options": False})(create) |
| 31 | + app.command(context_settings={"ignore_unknown_options": False})(update) |
| 32 | + app.command("list", context_settings={"ignore_unknown_options": False})(list_tasks_command) |
| 33 | + |
| 34 | + def version_callback(value: bool): |
| 35 | + if value: |
| 36 | + print(f"Task Badger CLI Version: {__version__}") |
| 37 | + raise typer.Exit() |
| 38 | + |
| 39 | + @app.command() |
| 40 | + def configure(ctx: typer.Context): |
| 41 | + """Update CLI configuration.""" |
| 42 | + config = ctx.meta["tb_config"] |
| 43 | + token = typer.prompt("API Key", default=config.token) |
| 44 | + parsed = _parse_token(token) |
| 45 | + if parsed: |
| 46 | + org_slug, project_slug, api_key = parsed |
| 47 | + print( |
| 48 | + f"Project key detected — organization: [green]{org_slug}[/green], " |
| 49 | + f"project: [green]{project_slug}[/green]" |
| 50 | + ) |
| 51 | + config.organization_slug = org_slug |
| 52 | + config.project_slug = project_slug |
| 53 | + config.token = token |
| 54 | + else: |
| 55 | + config.organization_slug = typer.prompt("Organization slug", default=config.organization_slug) |
| 56 | + config.project_slug = typer.prompt("Project slug", default=config.project_slug) |
| 57 | + config.token = token |
| 58 | + path = write_config(config) |
| 59 | + print(f"Config written to [green]{path}[/green]") |
| 60 | + |
| 61 | + @app.command() |
| 62 | + def docs(): |
| 63 | + """Open Task Badger docs in a browser.""" |
| 64 | + typer.launch("https://docs.taskbadger.net") |
| 65 | + |
| 66 | + @app.command() |
| 67 | + def info(ctx: typer.Context): |
| 68 | + """Show CLI configuration.""" |
| 69 | + config = ctx.meta["tb_config"] |
| 70 | + print(str(config)) |
| 71 | + |
| 72 | + @app.callback() |
| 73 | + def main( |
| 74 | + ctx: typer.Context, |
| 75 | + org: Optional[str] = typer.Option( |
| 76 | + None, |
| 77 | + "--org", |
| 78 | + "-o", |
| 79 | + metavar="TASKBADGER_ORG", |
| 80 | + show_default=False, |
| 81 | + help="Organization Slug. This will override values from the config file and environment variables.", |
| 82 | + ), |
| 83 | + project: Optional[str] = typer.Option( |
| 84 | + None, |
| 85 | + "--project", |
| 86 | + "-p", |
| 87 | + show_envvar=False, |
| 88 | + metavar="TASKBADGER_PROJECT", |
| 89 | + show_default=False, |
| 90 | + help="Project Slug. This will override values from the config file and environment variables.", |
| 91 | + ), |
| 92 | + version: Optional[bool] = typer.Option( # noqa |
| 93 | + None, |
| 94 | + "--version", |
| 95 | + callback=version_callback, |
| 96 | + is_eager=True, |
| 97 | + help="Show CLI Version", |
| 98 | + ), |
| 99 | + ): |
| 100 | + """ |
| 101 | + Task Badger CLI |
| 102 | + """ |
| 103 | + config = get_config(org=org, project=project) |
| 104 | + ctx.meta["tb_config"] = config |
| 105 | + |
| 106 | + if __name__ == "__main__": |
| 107 | + app() |
0 commit comments