Skip to content

Commit

Permalink
[resotocore][fix] Read search arguments more gracefully (#1356)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias committed Dec 21, 2022
1 parent e484f80 commit 5ef20b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions resotocore/resotocore/cli/command.py
Expand Up @@ -1220,8 +1220,17 @@ def parse_known(arg: str) -> Tuple[Dict[str, Any], str]:
parser.add_argument("--after", dest="after", default=None)
parser.add_argument("--before", dest="before", default=None)
parser.add_argument("--change", dest="change", default=None)
parsed, rest = parser.parse_known_args(list(args_parts_parser.parse(arg)))
return {k: v for k, v in vars(parsed).items() if v is not None}, " ".join(rest)
try:
# try to parse as many arguments as possible
args, remaining = args_parts_parser.parse_partial(arg)
# try to parse the parsed arguments
parsed, rest = parser.parse_known_args(list(args))
parsed_args = {k: v for k, v in vars(parsed).items() if v is not None}
# join the unparsed arguments and the remaining arg string
return parsed_args, " ".join(rest) + remaining
except Exception:
# coming here is totally fine - no args could be parsed
return {}, arg

@staticmethod
def argument_string(args: Dict[str, Any]) -> str:
Expand Down
6 changes: 6 additions & 0 deletions resotocore/tests/resotocore/cli/command_test.py
Expand Up @@ -677,6 +677,12 @@ async def test_jq_command(cli: CLI) -> None:
assert result[0] == ["foo", "foo"]


@pytest.mark.asyncio
async def test_execute_search_command(cli: CLI) -> None:
# regression test: this used to fail because the arg could not be parsed
await cli.execute_cli_command('execute_search (b= "0")', stream.list)


@pytest.mark.asyncio
async def test_aggregation_to_count_command(cli: CLI) -> None:
r = await cli.execute_cli_command("search all | count kind", stream.list)
Expand Down

0 comments on commit 5ef20b7

Please sign in to comment.