Add support for custom variables

This commit is contained in:
Ceda EI 2024-04-09 00:35:04 +05:30
parent f87b7661ee
commit 87c4f906b9
1 changed files with 21 additions and 3 deletions

View File

@ -1,5 +1,7 @@
import sys import sys
from typing import Optional from importlib import import_module
from pathlib import Path
from typing import Any, Optional
import click import click
from rich.console import Console from rich.console import Console
@ -15,12 +17,25 @@ error_console = Console(stderr=True, style="red bold")
@click.option( @click.option(
"-c", "--config", type=click.Path(exists=True, dir_okay=False, readable=True) "-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 @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) ctx.ensure_object(dict)
if config is None: if config is None:
config = str(default_config()) config = str(default_config())
ctx.obj["config"] = read_config(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): def _login_out(ctx: click.Context, login: bool):
@ -47,8 +62,11 @@ def _login_out(ctx: click.Context, login: bool):
variables_to_prompt.update( variables_to_prompt.update(
module.template_variables()["login" if login else "logout"] 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: for var in variables_to_prompt:
prompt = prompts.get(var, f"{var}:").strip() + " " prompt = prompts.get(var, f"{var}:").strip() + " "
responses[var] = console.input("[blue]" + prompt) responses[var] = console.input("[blue]" + prompt)