| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import argparse |
| 4 |
import pathlib |
| 5 |
import sys |
| 6 |
|
| 7 |
|
| 8 |
def load_params() -> tuple[bytes, str]: |
| 9 |
"""Load module arguments and profile when debugging an Ansible module.""" |
| 10 |
parser = argparse.ArgumentParser(description="Directly invoke an Ansible module for debugging.") |
| 11 |
parser.add_argument('args', nargs='?', help='module args JSON (file path or inline string)') |
| 12 |
parser.add_argument('--profile', default='legacy', help='profile for JSON decoding/encoding of args/response') |
| 13 |
|
| 14 |
parsed_args = parser.parse_args() |
| 15 |
|
| 16 |
args: str | None = parsed_args.args |
| 17 |
profile: str = parsed_args.profile |
| 18 |
|
| 19 |
if args: |
| 20 |
if (args_path := pathlib.Path(args)).is_file(): |
| 21 |
buffer = args_path.read_bytes() |
| 22 |
else: |
| 23 |
buffer = args.encode(errors='surrogateescape') |
| 24 |
else: |
| 25 |
if sys.stdin.isatty(): |
| 26 |
sys.stderr.write('Waiting for Ansible module JSON on STDIN...\n') |
| 27 |
sys.stderr.flush() |
| 28 |
|
| 29 |
buffer = sys.stdin.buffer.read() |
| 30 |
|
| 31 |
return buffer, profile |