Skip to content

Commit

Permalink
feat: read input data from piped input for validate
Browse files Browse the repository at this point in the history
  • Loading branch information
0hsn committed Nov 10, 2024
1 parent 1a02531 commit cb3f911
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
26 changes: 21 additions & 5 deletions chk/console/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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,
},
)

Expand Down
15 changes: 14 additions & 1 deletion chk/console/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Console service module
"""

import os
import sys
from typing import Any

import click
Expand All @@ -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 {}
Expand All @@ -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

0 comments on commit cb3f911

Please sign in to comment.