Skip to content

Commit

Permalink
[resotocore][fix] Fix infra apps aliases and error reporting (#1610)
Browse files Browse the repository at this point in the history
* [resotocore][fix] throw an error on unknown params when executing an app

* fix the help message generation
  • Loading branch information
meln1k authored May 26, 2023
1 parent 59a0c22 commit 760b1d9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
27 changes: 26 additions & 1 deletion resotocore/resotocore/cli/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ class AliasTemplate:
# only use args_description if the template does not use explicit parameters
args_description: Dict[str, str] = field(factory=dict)
allowed_in_source_position: bool = False
infra_app_parameters: Optional[Dict[str, Any]] = None # todo: remove this abomination

def render(self, props: Json) -> str:
return render_template(self.template, props)
Expand Down Expand Up @@ -378,6 +379,28 @@ def param_info(p: AliasTemplateParameter) -> str:
if self.description:
for line in self.description.splitlines():
desc += f"\n{indent}{line}"

if self.infra_app_parameters:

def param_info_infra_apps(name: str, arg_info: Dict[str, Any]) -> str:
default = f" [default: {arg_info.get('default')}]" if arg_info.get("default") else ""
return f"- `{name}`{default}: {arg_info.get('help')}"

arg_info = f"\n{indent}".join(
param_info_infra_apps(name, arg) for name, arg in self.infra_app_parameters.items()
)
result = dedent(
f"""
{self.name}: {self.info}
```shell
{self.name} {args}
```
{desc}
## Parameters
{arg_info}"""
)
return result

return dedent(
f"""
{self.name}: {self.info}
Expand All @@ -395,7 +418,7 @@ def param_info(p: AliasTemplateParameter) -> str:
## Example
```shell
# Executing this alias template
# Executing this command
> {self.name} {minimal}
# Will expand to this command
> {self.render({p.name: p.example_value() for p in self.parameters})}
Expand All @@ -417,6 +440,8 @@ def help_no_params_args(self) -> str:
)

def help(self) -> str:
if self.infra_app_parameters: # todo: remove this abomination
return self.help_with_params()
return self.help_with_params() if self.parameters else self.help_no_params_args()

def rendered_help(self, ctx: CLIContext) -> str:
Expand Down
13 changes: 4 additions & 9 deletions resotocore/resotocore/infra_apps/local_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,10 @@ async def execute(
"""
Runtime implementation that runs the app locally.
"""
try:
async for line in self.generate_template(graph, manifest, config, stdin, argv):
async with (await self._interpret_line(line, ctx)).stream() as streamer:
async for item in streamer:
yield item

except Exception as e:
msg = f"Error running infrastructure app: {e}"
log.exception(msg)
async for line in self.generate_template(graph, manifest, config, stdin, argv):
async with (await self._interpret_line(line, ctx)).stream() as streamer:
async for item in streamer:
yield item

async def generate_template(
self,
Expand Down
1 change: 1 addition & 0 deletions resotocore/resotocore/infra_apps/package_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def _setup_custom_commands(self, manifests: List[AppManifest]) -> None:
template=f"apps run {manifest.name}" + r" {{args}}",
description=manifest.readme,
allowed_in_source_position=True,
infra_app_parameters=manifest.args_schema,
)
self.add_command_alias(alias_template)

Expand Down
2 changes: 1 addition & 1 deletion resotocore/tests/resotocore/cli/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_alias_template() -> None:
## Example
```shell
# Executing this alias template
# Executing this command
> foo --a "test_a"
# Will expand to this command
> test_a | bv
Expand Down

0 comments on commit 760b1d9

Please sign in to comment.