|
10 | 10 | from cgr_src.executor import * |
11 | 11 | from cgr_src.state import * |
12 | 12 |
|
| 13 | +def _load(filepath, repo_dir=None, extra_vars=None, raise_on_error=False, inventory_files=None, |
| 14 | + vault_passphrase=None, vault_prompt=False, resolve_deferred_secrets=False): |
| 15 | + path=Path(filepath) |
| 16 | + if not path.exists(): |
| 17 | + if raise_on_error: raise ValueError(f"not found: {path}") |
| 18 | + print(red(f"error: not found: {path}"),file=sys.stderr); sys.exit(1) |
| 19 | + source=path.read_text() |
| 20 | + |
| 21 | + # Detect format by extension |
| 22 | + if path.suffix == ".cgr": |
| 23 | + try: ast=parse_cgr(source, str(path)) |
| 24 | + except CGRParseError as e: |
| 25 | + if raise_on_error: raise ValueError(e.msg) from e |
| 26 | + print(e.pretty(),file=sys.stderr); sys.exit(1) |
| 27 | + else: |
| 28 | + try: tokens=lex(source,str(path)) |
| 29 | + except LexError as e: |
| 30 | + if raise_on_error: raise ValueError(e.msg) from e |
| 31 | + print(red(f"Lex error: {e.msg}"),file=sys.stderr) |
| 32 | + if e.src: print(dim(f" {e.line:>4} │ ")+e.src,file=sys.stderr) |
| 33 | + sys.exit(1) |
| 34 | + try: ast=Parser(tokens,source,str(path)).parse() |
| 35 | + except ParseError as e: |
| 36 | + if raise_on_error: raise ValueError(e.msg) from e |
| 37 | + print(e.pretty(),file=sys.stderr); sys.exit(1) |
| 38 | + |
| 39 | + try: |
| 40 | + graph = resolve(ast, repo_dir=repo_dir, graph_file=str(path), extra_vars=extra_vars, |
| 41 | + inventory_files=inventory_files, vault_passphrase=vault_passphrase, |
| 42 | + vault_prompt=vault_prompt, |
| 43 | + resolve_deferred_secrets=resolve_deferred_secrets) |
| 44 | + _validate_script_paths(graph) |
| 45 | + return graph |
| 46 | + except ResolveError as e: |
| 47 | + if raise_on_error: raise ValueError(str(e)) from e |
| 48 | + print(red(f"error: {e}"),file=sys.stderr); sys.exit(1) |
| 49 | + |
13 | 50 | # ── Report command ───────────────────────────────────────────────────── |
14 | 51 |
|
15 | 52 | def cmd_report(graph_file: str, *, fmt: str = "table", output_file: str|None = None, |
|
0 commit comments