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 <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2024-09-18 15:55:20 -04:00
parent 76f4599460
commit f66cca92a9
4 changed files with 14 additions and 23 deletions

View File

@ -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

View File

@ -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):

View File

@ -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)

View File

@ -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)