diff --git a/inni/cli.py b/inni/cli.py index 6f710a0..531342a 100644 --- a/inni/cli.py +++ b/inni/cli.py @@ -1,5 +1,7 @@ import sys -from typing import Optional +from importlib import import_module +from pathlib import Path +from typing import Any, Optional import click from rich.console import Console @@ -15,12 +17,25 @@ error_console = Console(stderr=True, style="red bold") @click.option( "-c", "--config", type=click.Path(exists=True, dir_okay=False, readable=True) ) +@click.option( + "-v", "--vars-file", type=click.Path(exists=True, dir_okay=False, readable=True) +) @click.pass_context -def inni(ctx: click.Context, config: Optional[str]): +def inni(ctx: click.Context, config: Optional[str], vars_file: Optional[str]): ctx.ensure_object(dict) if config is None: config = str(default_config()) ctx.obj["config"] = read_config(config) + if vars_file is not None: + vars_module: Path = Path(vars_file) + else: + vars_module: Path = default_config().parent / "vars.py" + + if vars_module.exists(): + module_dir = vars_module.parent + sys.path.append(str(module_dir)) + ctx.obj["vars"] = import_module(vars_module.name.removesuffix(".py")) + sys.path.remove(str(module_dir)) def _login_out(ctx: click.Context, login: bool): @@ -47,8 +62,11 @@ def _login_out(ctx: click.Context, login: bool): variables_to_prompt.update( module.template_variables()["login" if login else "logout"] ) + variables_to_prompt.remove("vars") - responses = {} + responses: dict[str, Any] = { + "vars": ctx.obj.get("vars", {}), + } for var in variables_to_prompt: prompt = prompts.get(var, f"{var}:").strip() + " " responses[var] = console.input("[blue]" + prompt)