Add unknown_whitelist_manager/src/unknown_whitelist_manager/cli.py
This commit is contained in:
parent
ba5080c9c5
commit
9b99ca42f4
1 changed files with 60 additions and 0 deletions
|
|
@ -0,0 +1,60 @@
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from unknown_whitelist_manager import core
|
||||||
|
|
||||||
|
|
||||||
|
class CLIError(Exception):
|
||||||
|
"""Custom Exception for CLI errors."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
|
||||||
|
"""Parses command line arguments."""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Verwaltet die Unknown-Whitelist für CI-Prozesse."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--add-entry",
|
||||||
|
required=True,
|
||||||
|
help="Der einzufügende Whitelist-Eintrag als Textstring."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--whitelist-path",
|
||||||
|
required=False,
|
||||||
|
default="unknown_whitelist.json",
|
||||||
|
help="Pfad zur bestehenden Whitelist-Datei (default: unknown_whitelist.json)."
|
||||||
|
)
|
||||||
|
return parser.parse_args(argv)
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str] | None = None) -> None:
|
||||||
|
"""Entry point for CLI - processes arguments and updates whitelist."""
|
||||||
|
try:
|
||||||
|
args = parse_args(argv)
|
||||||
|
whitelist_path = Path(args.whitelist_path)
|
||||||
|
|
||||||
|
# Simple input validation as per constraint 'input_validation_required'
|
||||||
|
entry_text = args.add_entry.strip()
|
||||||
|
if not entry_text:
|
||||||
|
raise CLIError("Der Eintrag darf nicht leer sein.")
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class WhitelistEntry:
|
||||||
|
entry: str
|
||||||
|
|
||||||
|
entry = WhitelistEntry(entry=entry_text)
|
||||||
|
core.update_whitelist(str(whitelist_path), entry)
|
||||||
|
|
||||||
|
except CLIError as e:
|
||||||
|
print(f"Fehler: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Unerwarteter Fehler: {e}", file=sys.stderr)
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue