From cb3f911585b95fe51dba2f1f4d5d482f9c91bc20 Mon Sep 17 00:00:00 2001 From: HSN Date: Sun, 10 Nov 2024 16:23:07 +0600 Subject: [PATCH] feat: read input data from piped input for validate --- chk/console/main.py | 26 +++++++++++++++++++++----- chk/console/services.py | 15 ++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/chk/console/main.py b/chk/console/main.py index c5edf926..dd9fac68 100644 --- a/chk/console/main.py +++ b/chk/console/main.py @@ -8,10 +8,12 @@ from chk.console.services import ( after_hook, combine_initial_variables, + get_stdin, load_variables_as_dict, setup_logger, ) from chk.infrastructure.file_loader import ExecuteContext, FileContext +from chk.infrastructure.logging import with_catch_log VAR_ERROR_MSG = "-V, --variables accept values as JSON object" @@ -84,9 +86,15 @@ def fetch(cctx: click.Context, file: str, no_format: bool, variables: str) -> No ) @click.option("-V", "--variables", type=str, help="Pass variable(s) as JSON object") @click.option("-D", "--data", type=str, help="Pass data as JSON") +@click.option("-Di", "--data-in", is_flag=True, help="Pass data as JSON [from pipe]") @click.pass_context def validate( - cctx: click.Context, file: str, no_format: bool, variables: str, data: str + cctx: click.Context, + file: str, + no_format: bool, + variables: str, + data: str, + data_in: bool, ) -> None: """\b Command to run Validation specification files. @@ -97,6 +105,17 @@ def validate( ctx: FileContext = FileContext.from_file(file) + with with_catch_log(): + _data = ( + load_variables_as_dict( + get_stdin(), except_msg="-Di, --data-in: Pass data as JSON [from pipe]" + ) + if data_in + else load_variables_as_dict( + data, except_msg="-D, --data: Pass data as JSON" + ) + ) + execution_ctx = ExecuteContext( { "dump": True, @@ -108,10 +127,7 @@ def validate( variables, except_msg=VAR_ERROR_MSG, ), - "data": load_variables_as_dict( - data, - except_msg="-D, --data accept values as JSON object", - ), + "data": _data, }, ) diff --git a/chk/console/services.py b/chk/console/services.py index a6d069ab..07934682 100644 --- a/chk/console/services.py +++ b/chk/console/services.py @@ -2,6 +2,8 @@ Console service module """ +import os +import sys from typing import Any import click @@ -18,7 +20,7 @@ def load_variables_as_dict(json_str: str, **kwargs: Any) -> dict: try: return FileLoader.load_json_from_str(json_str) except JsonDecodingError as err: - message = kwargs.get("except_msg") or "JSON loading error." + message = kwargs.get("except_msg", "JSON loading error.") raise click.UsageError(str(message)) from err return {} @@ -43,3 +45,14 @@ def setup_logger(should_log: bool) -> None: if should_log: log_file = LoggingManager.create_new_log_file() LoggingManager.setup_loguru(log_file) + + +def get_stdin() -> str: + """This will get stdin piped input *if exists*""" + + raw_str = "" + with os.fdopen(sys.stdin.fileno(), "rb", buffering=0) as stdin: + if not stdin.seekable(): + raw_str = "".join([_.decode("utf-8") for _ in stdin.readlines()]) + + return raw_str