Skip to content

Commit

Permalink
[resotocore][fix] Pass and use adjusted context to rewrite (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias authored May 9, 2023
1 parent c206ed2 commit b750ca9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 5 additions & 5 deletions resotocore/resotocore/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,15 +461,15 @@ async def combine_query_parts(
return ctx_wq, [*query_parts, *remaining]
return ctx, commands

def rewrite_command_line(cmds: List[ExecutableCommand]) -> List[ExecutableCommand]:
def rewrite_command_line(cmds: List[ExecutableCommand], ctx: CLIContext) -> List[ExecutableCommand]:
"""
Rewrite the command line to make it more user-friendly.
Rules:
- add the list command if no output format is defined
- add a format to write commands if no output format is defined
- report benchmark run will be formatted as benchmark result automatically
"""
if context.env.get("no_rewrite") or len(cmds) == 0:
if ctx.env.get("no_rewrite") or len(cmds) == 0:
return cmds
first_cmd = cmds[0]
last_cmd = cmds[-1]
Expand All @@ -480,10 +480,10 @@ def no_format() -> bool:
return not any(c for c in result if isinstance(c.command, (OutputTransformer, PreserveOutputFormat)))

def fmt_benchmark() -> ExecutableCommand:
return self.command("format", "--benchmark-result", context)
return self.command("format", "--benchmark-result", ctx)

def fmt_list() -> ExecutableCommand:
return self.command("list", None, context)
return self.command("list", None, ctx)

# benchmark run as single command is rewritten to benchmark run | format --benchmark-result
if single and isinstance(single.command, ReportCommand) and ReportCommand.is_run_action(single.arg):
Expand Down Expand Up @@ -513,7 +513,7 @@ async def parse_line(parsed: ParsedCommands) -> ParsedCommandLine:
ctx, commands = await combine_query_parts(
[self.command(c.cmd, c.args, ctx, position=i) for i, c in enumerate(parsed.commands)], ctx
)
rewritten = rewrite_command_line(commands)
rewritten = rewrite_command_line(commands, ctx)
not_met = [r for cmd in rewritten for r in cmd.action.required if r.name not in context.uploaded_files]
envelope = {k: v for cmd in rewritten for k, v in cmd.action.envelope.items()}
return ParsedCommandLine(ctx, parsed, rewritten, not_met, envelope)
Expand Down
4 changes: 4 additions & 0 deletions resotocore/tests/resotocore/cli/command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,10 @@ async def test_list_command(cli: CLI) -> None:
result = await cli.execute_cli_command(f'search is (foo) and identifier=="4" | {list_cmd}', stream.list)
assert result[0] == ["si=0, some_string=hello"]

# list is added automatically when no output renderer is defined and has the same behaviour as if it was given
result = await cli.execute_cli_command('search is (foo) and identifier=="4" sort some_int', stream.list)
assert result[0][0].startswith("kind=foo, identifier=4, some_int=0, age=")

# List is using the correct type
props = dict(id="test", a="a", b=True, c=False, d=None, e=12, f=1.234, reported={})
result = await cli.execute_cli_command(f"json {json.dumps(props)}" " | list a,b,c,d,e,f", stream.list)
Expand Down

0 comments on commit b750ca9

Please sign in to comment.