Skip to content

Commit b85d944

Browse files
committed
fix(cli): always resolve paths passed via the cli to handle relative paths
1 parent 170ec3b commit b85d944

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

packages/debmagic/src/debmagic/cli/_config.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import argparse
22
from pathlib import Path
3-
from typing import ClassVar
3+
from typing import Annotated, ClassVar
44

5-
from pydantic import Field
5+
from pydantic import AfterValidator, Field
66
from pydantic_settings import (
77
BaseSettings,
88
CliApp,
@@ -14,19 +14,23 @@
1414
from ._build_driver.config import BuildDriverConfig
1515

1616

17-
# TODO: figure out how to get cli_avoid_json with the nested driver struct to work properly
17+
def resolve_path(p: Path) -> Path:
18+
return p.resolve()
19+
20+
1821
class DebmagicConfig(BaseSettings, cli_kebab_case=True, cli_avoid_json=True):
1922
_config_file_paths: ClassVar[list[Path]] = []
2023

2124
driver_config: BuildDriverConfig = BuildDriverConfig()
2225

23-
temp_build_dir: Path = Field(
24-
default=Path("/tmp/debmagic"),
25-
description="Temporary directory on the local machine used as root directory for all package builds",
26-
)
26+
temp_build_dir: Annotated[
27+
Path,
28+
Field(description="Temporary directory on the local machine used as root directory for all package builds"),
29+
AfterValidator(resolve_path),
30+
] = Path("/tmp/debmagic")
2731

28-
dry_run: bool = Field(
29-
default=False, description="don't actually run anything that changes the system/package state"
32+
dry_run: Annotated[bool, Field(description="don't actually run anything that changes the system/package state")] = (
33+
False
3034
)
3135

3236
@classmethod

packages/debmagic/src/debmagic/cli/_main.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
from ._version import VERSION
1111

1212

13+
def arg_resolved_path(arg: str) -> Path:
14+
return Path(arg).resolve()
15+
16+
1317
def _create_parser() -> argparse.ArgumentParser:
1418
cli = argparse.ArgumentParser(description="Debmagic")
1519
sp = cli.add_subparsers(dest="operation")
@@ -19,31 +23,27 @@ def _create_parser() -> argparse.ArgumentParser:
1923
sp.add_parser("version", help="Print the version information and exit")
2024

2125
common_cli = get_config_argparser()
22-
common_cli.add_argument("-c", "--config", type=Path, help="Path to a config file")
26+
common_cli.add_argument("-c", "--config", type=arg_resolved_path, help="Path to a config file")
2327

2428
build_cli = sp.add_parser(
2529
"build", parents=[common_cli], help="Build a debian package with the selected containerization driver"
2630
)
2731
build_cli.add_argument("--driver", choices=SUPPORTED_BUILD_DRIVERS, required=True)
28-
build_cli.add_argument("-s", "--source-dir", type=Path, default=Path.cwd())
29-
build_cli.add_argument("-o", "--output-dir", type=Path, default=Path.cwd())
32+
build_cli.add_argument("-s", "--source-dir", type=arg_resolved_path, default=Path.cwd())
33+
build_cli.add_argument("-o", "--output-dir", type=arg_resolved_path, default=Path.cwd())
3034

3135
sp.add_parser("check", parents=[common_cli], help="Run linters (e.g. lintian)")
3236

3337
shell_cli = sp.add_parser("shell", parents=[common_cli], help="Attach a shell to a running debmagic build")
34-
shell_cli.add_argument("-s", "--source-dir", type=Path, default=Path.cwd())
38+
shell_cli.add_argument("-s", "--source-dir", type=arg_resolved_path, default=Path.cwd())
3539

3640
sp.add_parser("test", parents=[common_cli], help="Run package tests")
3741
return cli
3842

3943

4044
def main(passed_args: Sequence[str] | None = None):
4145
cli = _create_parser()
42-
args, unknown_args = cli.parse_known_args(passed_args)
43-
44-
if len(unknown_args) > 0 and args.operation != "build":
45-
# TODO: proper validation and printout -> maybe differentiate between build subcommand and others???
46-
raise RuntimeError("unknown arguments passed")
46+
args = cli.parse_args(passed_args)
4747

4848
config_file_paths: list[Path] = []
4949
if args.config:

0 commit comments

Comments
 (0)