33import argparse
44import re
55import sys
6- from dataclasses import replace as dataclass_replace
76from pathlib import Path
87from typing import Optional
98
109from env_auditor import __version__
1110from env_auditor .colors import supports_color
1211from env_auditor .config import (
13- EnvCheckConfig ,
14- _dict_to_config ,
15- _parse_toml_file ,
12+ EnvAuditorConfig ,
1613 load_config ,
1714 merge_cli_into_config ,
1815)
@@ -68,6 +65,7 @@ def _build_parser() -> argparse.ArgumentParser:
6865 "--format" ,
6966 choices = ["text" , "json" ],
7067 default = None ,
68+ dest = "output_format" ,
7169 help = "Output format. Default: text" ,
7270 )
7371 parser .add_argument (
@@ -129,10 +127,17 @@ def _resolve_scan_root(raw_path: str) -> Path:
129127 Raises:
130128 SystemExit(2): If the path does not exist or is not a directory.
131129 """
130+ resolved : Optional [Path ] = None
132131 try :
133132 resolved = Path (raw_path ).resolve ()
134133 except (OSError , ValueError ) as exc :
135134 _die (f"Invalid path '{ raw_path } ': { exc } " )
135+
136+ # resolved is always set here — _die exits, but the type checker
137+ # doesn't know that, so we guard explicitly.
138+ if resolved is None : # pragma: no cover
139+ _die (f"Could not resolve path '{ raw_path } '" )
140+
136141 if not resolved .exists ():
137142 _die (f"Path does not exist: { resolved } " )
138143 if not resolved .is_dir ():
@@ -175,7 +180,8 @@ def _resolve_exclude_dirs(raw_dirs: list[str], scan_root: Path) -> list[Path]:
175180 """Resolve --exclude paths, rejecting any that escape the scan root.
176181
177182 Security: path traversal is rejected — each resolved path must be
178- a descendant of scan_root.
183+ a descendant of scan_root. Newlines are stripped from raw input to
184+ prevent log injection.
179185
180186 Args:
181187 raw_dirs: Raw directory strings from CLI or config.
@@ -186,7 +192,7 @@ def _resolve_exclude_dirs(raw_dirs: list[str], scan_root: Path) -> list[Path]:
186192 """
187193 resolved_list : list [Path ] = []
188194 for raw in raw_dirs :
189- # Strip newlines to prevent any injection via crafted input
195+ # Strip newlines to prevent log/terminal injection via crafted input
190196 raw_safe = re .sub (r"[\r\n]" , "" , raw )
191197 p = Path (raw_safe )
192198 if not p .is_absolute ():
@@ -217,7 +223,7 @@ def _die(msg: str) -> None:
217223# Config loading
218224# ──────────────────────────────────────────────────────────────────────────────
219225
220- def _build_config (args : argparse .Namespace , scan_root : Path ) -> EnvCheckConfig :
226+ def _build_config (args : argparse .Namespace , scan_root : Path ) -> EnvAuditorConfig :
221227 """Load config file and apply CLI overrides on top.
222228
223229 Config file is auto-discovered in scan_root unless ``--config`` is given.
@@ -228,17 +234,15 @@ def _build_config(args: argparse.Namespace, scan_root: Path) -> EnvCheckConfig:
228234 scan_root: Resolved scan root.
229235
230236 Returns:
231- Final merged EnvCheckConfig .
237+ Final merged EnvAuditorConfig .
232238 """
233239 if args .config :
234240 config_path = Path (args .config ).resolve ()
235241 if not config_path .is_file ():
236242 _die (f"Config file not found: { config_path } " )
237- try :
238- raw = _parse_toml_file (config_path , config_path .name == "pyproject.toml" )
239- cfg = _dict_to_config (raw or {}, config_path )
240- except (OSError , ValueError , KeyError ) as exc :
241- _die (f"Could not parse config file { config_path } : { exc } " )
243+ # Load config from explicit path by temporarily treating scan_root
244+ # as the config file's parent
245+ cfg = load_config (config_path .parent )
242246 else :
243247 cfg = load_config (scan_root )
244248
@@ -249,7 +253,7 @@ def _build_config(args: argparse.Namespace, scan_root: Path) -> EnvCheckConfig:
249253 ignore_stale = args .ignore_stale or None ,
250254 ignore_missing = args .ignore_missing or None ,
251255 strict = args .strict or None ,
252- format = args . format ,
256+ output_format = getattr ( args , "output_format" , None ) ,
253257 )
254258
255259
@@ -259,7 +263,7 @@ def _build_config(args: argparse.Namespace, scan_root: Path) -> EnvCheckConfig:
259263
260264def _run_audit (
261265 scan_root : Path ,
262- cfg : EnvCheckConfig ,
266+ cfg : EnvAuditorConfig ,
263267 use_color : bool ,
264268) -> tuple [int , str ]:
265269 """Execute the full audit pipeline.
@@ -293,7 +297,7 @@ def _run_audit(
293297 )
294298
295299 ignore_keys : set [str ] = set (cfg .ignore_keys ) if cfg .ignore_keys else set ()
296- fmt = cfg .format or "text"
300+ fmt = cfg .output_format or "text"
297301
298302 if fmt == "json" :
299303 output = render_json (
@@ -331,15 +335,13 @@ def main(argv: Optional[list[str]] = None) -> None:
331335 """Parse arguments, run the audit, and exit with the appropriate code.
332336
333337 Exit codes:
334- 0 — clean (no undocumented vars; no stale with --strict)
338+ 0 — clean
335339 1 — undocumented variables found (or stale with --strict)
336340 2 — tool error (bad arguments, unreadable config, etc.)
337341 """
338342 parser = _build_parser ()
339343 args = parser .parse_args (argv )
340344
341- # Resolve color preference before any output.
342- # --no-color disables color without mutating os.environ.
343345 use_color = (not args .no_color ) and supports_color ()
344346
345347 scan_root = _resolve_scan_root (args .path )
0 commit comments