Skip to content

Commit 332b69e

Browse files
committed
Create a global settings file for the CLI
1 parent 331ba33 commit 332b69e

File tree

3 files changed

+73
-65
lines changed

3 files changed

+73
-65
lines changed

dlite_entities_service/utils_cli/config.py

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
from rich.console import Console
1818

1919
from dlite_entities_service.service.config import CONFIG
20+
from dlite_entities_service.utils_cli.global_settings import STATUS
2021

2122
ERROR_CONSOLE = Console(stderr=True)
2223
CLI_DOTENV_FILE = Path(__file__).resolve().parent / ".env"
2324
SERVICE_DOTENV_FILE = Path(__file__).resolve().parent.parent.parent / ".env"
2425

2526
APP = typer.Typer(
2627
name=__file__.rsplit("/", 1)[-1].replace(".py", ""),
28+
help="Manage configuration options.",
2729
no_args_is_help=True,
2830
invoke_without_command=True,
2931
)
3032

31-
STATUS = {"use_service_dotenv": False}
32-
3333

3434
class ConfigFields(str, Enum):
3535
"""Configuration options."""
@@ -111,7 +111,7 @@ def unset(
111111
show_default=False,
112112
),
113113
) -> None:
114-
"""Unset a configuration option."""
114+
"""Unset a single configuration option."""
115115
if STATUS["use_service_dotenv"]:
116116
dotenv_file = SERVICE_DOTENV_FILE
117117
else:
@@ -121,6 +121,26 @@ def unset(
121121
print(f"Unset {CONFIG.Config.env_prefix}{key}.")
122122

123123

124+
@APP.command()
125+
def unset_all() -> None:
126+
"""Unset all configuration options."""
127+
typer.confirm(
128+
"Are you sure you want to unset (remove) all configuration options in "
129+
f"{'Service' if STATUS['use_service_dotenv'] else 'CLI'}-specific .env file?",
130+
abort=True,
131+
)
132+
133+
if STATUS["use_service_dotenv"]:
134+
dotenv_file = SERVICE_DOTENV_FILE
135+
else:
136+
dotenv_file = CLI_DOTENV_FILE
137+
if dotenv_file.exists():
138+
dotenv_file.unlink()
139+
print(f"Unset all configuration options. (Removed {dotenv_file}.)")
140+
else:
141+
print(f"Unset all configuration options. ({dotenv_file} file not found.)")
142+
143+
124144
@APP.command()
125145
def show(
126146
reveal_sensitive: bool = typer.Option(
@@ -151,44 +171,3 @@ def show(
151171
if not reveal_sensitive and key.is_sensitive():
152172
value = "***"
153173
print(f"[bold]{CONFIG.Config.env_prefix}{key}[/bold]: {value}")
154-
155-
156-
@APP.callback()
157-
def main(
158-
use_service_dotenv: bool = typer.Option(
159-
False,
160-
"--use-service-dotenv/--use-cli-dotenv",
161-
help=(
162-
"Use the .env file also used for the DLite Entities Service or one only "
163-
"for the CLI."
164-
),
165-
is_flag=True,
166-
),
167-
unset_all: bool = typer.Option(
168-
False,
169-
"--unset-all",
170-
help="Unset (remove) all configuration options in dotenv file.",
171-
show_default=False,
172-
is_flag=True,
173-
),
174-
) -> None:
175-
"""Set DLite entities service configuration options."""
176-
STATUS["use_service_dotenv"] = use_service_dotenv
177-
178-
if unset_all:
179-
typer.confirm(
180-
"Are you sure you want to unset (remove) all configuration options in "
181-
f"{'Service' if use_service_dotenv else 'CLI'}-specific .env file?",
182-
abort=True,
183-
)
184-
185-
if use_service_dotenv:
186-
dotenv_file = SERVICE_DOTENV_FILE
187-
else:
188-
dotenv_file = CLI_DOTENV_FILE
189-
190-
if dotenv_file.exists():
191-
dotenv_file.unlink()
192-
print(f"Removed {dotenv_file}.")
193-
else:
194-
print(f"No {dotenv_file} file found.")
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Global settings for the CLI."""
2+
from pathlib import Path
3+
from typing import Optional
4+
5+
try:
6+
import typer
7+
except ImportError as exc:
8+
raise ImportError(
9+
"Please install the DLite entities service utility CLI with "
10+
f"'pip install {Path(__file__).resolve().parent.parent.parent.resolve()}[cli]'"
11+
) from exc
12+
13+
from rich import print # pylint: disable=redefined-builtin
14+
15+
from dlite_entities_service import __version__
16+
17+
STATUS = {"use_service_dotenv": False}
18+
19+
20+
def _print_version(value: bool) -> None:
21+
"""Print version and exit."""
22+
if value:
23+
print(f"dlite-entities-service version: {__version__}")
24+
raise typer.Exit()
25+
26+
27+
def global_options(
28+
_: Optional[bool] = typer.Option(
29+
None,
30+
"--version",
31+
help="Show version and exit",
32+
is_eager=True,
33+
callback=_print_version,
34+
),
35+
use_service_dotenv: bool = typer.Option(
36+
False,
37+
"--use-service-dotenv/--use-cli-dotenv",
38+
help=(
39+
"Use the .env file also used for the DLite Entities Service or one only "
40+
"for the CLI."
41+
),
42+
is_flag=True,
43+
rich_help_panel="Global options",
44+
),
45+
) -> None:
46+
"""Global options for the CLI."""
47+
STATUS["use_service_dotenv"] = use_service_dotenv

dlite_entities_service/utils_cli/main.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
get_collection,
2828
)
2929
from dlite_entities_service.utils_cli.config import APP as config_APP
30+
from dlite_entities_service.utils_cli.global_settings import global_options
3031

3132
if TYPE_CHECKING: # pragma: no cover
3233
from typing import Any
@@ -50,15 +51,9 @@ class EntityFileFormats(str, Enum):
5051
help="DLite entities service utility CLI",
5152
no_args_is_help=True,
5253
pretty_exceptions_show_locals=False,
54+
callback=global_options,
5355
)
54-
APP.add_typer(config_APP)
55-
56-
57-
def _print_version(value: bool) -> None:
58-
"""Print version and exit."""
59-
if value:
60-
print(f"dlite-entities-service version: {__version__}")
61-
raise typer.Exit()
56+
APP.add_typer(config_APP, callback=global_options)
6257

6358

6459
def _get_backend() -> "Collection":
@@ -77,19 +72,6 @@ def _get_backend() -> "Collection":
7772
return ENTITIES_COLLECTION
7873

7974

80-
@APP.callback()
81-
def main(
82-
_: Optional[bool] = typer.Option(
83-
None,
84-
"--version",
85-
help="Show version and exit",
86-
is_eager=True,
87-
callback=_print_version,
88-
),
89-
) -> None:
90-
"""DLite entities service utility CLI."""
91-
92-
9375
@APP.command(no_args_is_help=True)
9476
def upload(
9577
filepaths: Optional[list[Path]] = typer.Option(

0 commit comments

Comments
 (0)