Coverage for b4_backup/cli/init.py: 100%
22 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-18 22:40 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-18 22:40 +0000
1"""Contains the base part of the CLI."""
3import logging.config
4from pathlib import Path
6import omegaconf
7import rich
8import typer
10from b4_backup import utils
12app = typer.Typer(
13 pretty_exceptions_enable=False,
14 no_args_is_help=True,
15)
18def _version_callback(value: bool):
19 if value:
20 import importlib.metadata
22 typer.echo(importlib.metadata.version("b4_backup"))
23 raise typer.Exit()
26@app.callback()
27def init(
28 ctx: typer.Context,
29 config_path: Path = typer.Option(
30 utils.DEFAULT_CONFIG,
31 "--config",
32 "-c",
33 help="Path to the config file",
34 ),
35 options: list[str] = typer.Option([], "--option", "-o", help="Override values from the config"),
36 _version: bool = typer.Option(
37 False,
38 "--version",
39 "-v",
40 callback=_version_callback,
41 is_eager=True,
42 help="Print the current version and exit",
43 ),
44):
45 """Backup and restore btrfs subvolumes using btrfs-progs."""
46 try:
47 config = utils.load_config(config_path, options)
48 except omegaconf.errors.OmegaConfBaseException as exc:
49 rich.print(f"[red]You got an error in your configuration file {config_path}:")
50 rich.print(exc)
51 raise typer.Exit(1) from exc
53 logging.config.dictConfig(config.logging)
55 ctx.obj = config