Skip to content

Commit

Permalink
[resotoshell][fix] Parse argument commands (#1642)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias committed Jun 7, 2023
1 parent b06fedc commit 3ee2c1e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
16 changes: 15 additions & 1 deletion resotoshell/resotoshell/promptsession.py
Expand Up @@ -28,8 +28,9 @@
from resotoclient.async_client import ResotoClient
from resotoclient.models import Property

from resotolib.json import from_json
from resotolib.json import from_json, register_json
from resotolib.logger import log
from resotolib.types import JsonElement


def cut_document_remaining(document: Document, span: Tuple[int, int]) -> Document:
Expand Down Expand Up @@ -827,3 +828,16 @@ def path(p: Property) -> List[str]:
exc_info=ex,
)
return [], [], []


# cattrs needs help with parsing the args
def parse_args(js: JsonElement) -> ArgsInfo:
if isinstance(js, list):
return [from_json(v, ArgInfo) for v in js]
elif isinstance(js, dict):
return {k: parse_args(v) for k, v in js.items()}
else:
raise ValueError(f"Expected list or dict, got {js}")


register_json(ArgsInfo, from_json_fn=parse_args) # type: ignore
90 changes: 90 additions & 0 deletions resotoshell/test/test_promptsession.py
Expand Up @@ -9,6 +9,7 @@
)
from prompt_toolkit.document import Document

from resotolib.json import from_json
from resotoshell.promptsession import (
CommandLineCompleter,
SearchCompleter,
Expand Down Expand Up @@ -122,6 +123,95 @@ def complete(part: str, completer: Completer, return_meta: bool = False) -> Set[
}


def test_parse_command_info() -> None:
cmd = {
"args": {
"activate": [
{
"can_occur_multiple_times": False,
"expects_value": False,
"help_text": "<job-id>",
"name": None,
"option_group": None,
"possible_values": [],
"value_hint": None,
}
],
"add": [
{
"can_occur_multiple_times": False,
"expects_value": True,
"help_text": None,
"name": "--id",
"option_group": None,
"possible_values": [],
"value_hint": None,
}
],
"deactivate": [
{
"can_occur_multiple_times": False,
"expects_value": False,
"help_text": "<job-id>",
"name": None,
"option_group": None,
"possible_values": [],
"value_hint": None,
}
],
"delete": [
{
"can_occur_multiple_times": False,
"expects_value": False,
"help_text": "<job-id>",
"name": None,
"option_group": None,
"possible_values": [],
"value_hint": None,
}
],
"list": [],
"run": [
{
"can_occur_multiple_times": False,
"expects_value": False,
"help_text": "<job-id>",
"name": None,
"option_group": None,
"possible_values": [],
"value_hint": None,
}
],
},
"help": "some help",
"info": "Manage all jobs.",
"name": "jobs",
"source": True,
}
app = {"args": [], "help": "", "info": "Tag Validator app for Resoto.", "name": "tagvalidator", "source": True}
template = {
"args": [
{
"can_occur_multiple_times": False,
"expects_value": True,
"help_text": "[required] Alert title",
"name": "--title",
"option_group": None,
"possible_values": [],
"value_hint": None,
}
],
"help": "some help",
"info": "Send the result of a search to Discord",
"name": "discord",
"source": False,
}

assert from_json(cmd, CommandInfo) is not None
assert from_json(app, CommandInfo) is not None
assert from_json(template, CommandInfo) is not None


def test_doc_ext() -> None:
d = Document("Sentence 1. Things I want to say", 32)
ext = DocumentExtension(d, re.compile(r"\s*[.]\s*"))
Expand Down

0 comments on commit 3ee2c1e

Please sign in to comment.