From f66cca92a981b1ded8dc28af6a63a134a78732b9 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Wed, 18 Sep 2024 15:55:20 -0400 Subject: [PATCH] cli: Make --xml option parsing less special We can make `--xml` fit the common xml cli option paradigm, which less us drop a whole bunch of special handling in virt-xml Signed-off-by: Cole Robinson --- tests/test_cli.py | 2 +- virtinst/cli.py | 25 ++++++++++++------------- virtinst/virtinstall.py | 1 - virtinst/virtxml.py | 9 +-------- 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index e9c00bf5e..e4c21f4df 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1413,7 +1413,7 @@ c.add_invalid("test-for-virtxml --remove-device --host-device 1 --update --confi c.add_invalid("test-for-virtxml --edit --graphics password=foo,keymap= --update --confirm", input_text="yes", grep="(not supported by the connection driver: virDomainUpdateDeviceFlags|persistent update of device 'graphics' is not supported)") c.add_invalid("--build-xml --memory 10,maxmemory=20", grep="--build-xml not supported for --memory") c.add_invalid("test-state-shutoff --edit sparse=no --disk path=blah", grep="Don't know how to match device type 'disk' property 'sparse'") -c.add_invalid("test --add-device --xml ./@foo=bar", grep="--xml can only be used with --edit") +c.add_invalid("test --add-device --xml ./@foo=bar", grep="Cannot use --add-device with --xml") c.add_invalid("test-for-virtxml --edit --boot refresh-machine-type=yes", grep="Don't know how to refresh") c.add_compare("test --print-xml --edit --vcpus 7", "print-xml") # test --print-xml c.add_compare("--edit --cpu host-passthrough", "stdin-edit", input_file=(_VIRTXMLDIR + "virtxml-stdin-edit.xml")) # stdin test diff --git a/virtinst/cli.py b/virtinst/cli.py index eed11b57f..0bd866c5b 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -493,7 +493,7 @@ def fail_conflicting(option1, option2): def _get_completer_parsers(): return VIRT_PARSERS + [ParserCheck, ParserLocation, - ParserUnattended, ParserInstall, ParserCloudInit, ParserXML, + ParserUnattended, ParserInstall, ParserCloudInit, ParserOSVariant] @@ -947,6 +947,7 @@ def add_os_variant_option(parser, virtinstall): def add_xml_option(grp): + ParserXML.register() grp.add_argument("--xml", action="append", default=[], help=_("Perform raw XML XPath options on the final XML. Example:\n" "--xml ./cpu/@mode=host-passthrough\n" @@ -1608,23 +1609,21 @@ class ParserXML(VirtCLIParser): cls.add_arg("xpath.create", "xpath_create", can_comma=True) cls.add_arg("xpath.value", "xpath_value", can_comma=True) - def _parse(self, inst): + def parse(self, inst): + """ + Parse --xml option string into XMLManualAction instances and append + to guest.xml_actions. + """ + inst = self.guest.xml_actions.new() + + # Default `--xml FOO` to `--xml xpath.set=FOO` if not self.optstr.startswith("xpath."): self.optdict.clear() self.optdict["xpath.set"] = self.optstr - super()._parse(inst) + super().parse(inst) - -def parse_xmlcli(guest, parservalue): - """ - Parse --xml option string into XMLManualAction instances and append - to guest.xml_actions. - """ - for optstr in parservalue: - inst = guest.xml_actions.new() - ParserXML(optstr).parse(inst) - guest.xml_actions.append(inst) + self.guest.xml_actions.append(inst) def _add_xpath_args(cls): diff --git a/virtinst/virtinstall.py b/virtinst/virtinstall.py index 065f8847b..3162c8a0b 100644 --- a/virtinst/virtinstall.py +++ b/virtinst/virtinstall.py @@ -664,7 +664,6 @@ def build_guest_instance(conn, options): # default disk paths are generated based on VM name set_cli_default_name(guest) cli.run_all_parsers(options, guest) - cli.parse_xmlcli(guest, options.xml) set_cli_defaults(options, guest) installer.set_install_defaults(guest) diff --git a/virtinst/virtxml.py b/virtinst/virtxml.py index 202e87ca7..d8ab01f2a 100644 --- a/virtinst/virtxml.py +++ b/virtinst/virtxml.py @@ -109,9 +109,6 @@ def validate_action(action, conn, options): fail(_("--build-xml not supported for --%s") % action.parserclass.cli_arg_name) - if action.parserclass is cli.ParserXML and not action.is_edit: - fail(_("--xml can only be used with --edit")) - stub_guest = Guest(conn) if not action.parserclass.prop_is_list(stub_guest): if action.is_remove_device: @@ -148,7 +145,7 @@ def check_action_collision(options): def check_xmlopt_collision(options): collisions = [] - for parserclass in cli.VIRT_PARSERS + [cli.ParserXML]: + for parserclass in cli.VIRT_PARSERS: value = getattr(options, parserclass.cli_arg_name) if value: collisions.append((parserclass, value)) @@ -236,10 +233,6 @@ def action_edit(action, guest): parservalue = action.parservalue selector = action.selector - if parserclass is cli.ParserXML: - cli.parse_xmlcli(guest, parservalue) - return [] - if parserclass.guest_propname: inst = _find_objects_to_edit(guest, "edit", selector, parserclass)