@@ -26,6 +26,36 @@ pub(super) fn virsh_command(connect_uri: Option<&str>) -> Result<std::process::C
2626 Ok ( cmd)
2727}
2828
29+ /// Run a virsh command that returns XML and parse it directly
30+ ///
31+ /// This helper function consolidates the common pattern of:
32+ /// 1. Running a virsh command with arguments
33+ /// 2. Checking if the command succeeded
34+ /// 3. Parsing the XML output from the stdout bytes
35+ ///
36+ /// # Arguments
37+ /// * `connect_uri` - Optional libvirt connection URI
38+ /// * `args` - Arguments to pass to virsh
39+ ///
40+ /// # Returns
41+ /// The parsed XML as an XmlNode
42+ pub fn run_virsh_xml ( connect_uri : Option < & str > , args : & [ & str ] ) -> Result < xml_utils:: XmlNode > {
43+ let mut cmd = virsh_command ( connect_uri) ?;
44+ cmd. args ( args) ;
45+
46+ let output = cmd. output ( ) . context ( "Failed to run virsh" ) ?;
47+
48+ if !output. status . success ( ) {
49+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
50+ return Err ( eyre:: eyre!( "virsh command failed: {}" , stderr) ) ;
51+ }
52+
53+ // Parse XML directly from bytes
54+ let xml = std:: str:: from_utf8 ( & output. stdout ) . context ( "Invalid UTF-8 in virsh output" ) ?;
55+
56+ xml_utils:: parse_xml_dom ( xml) . context ( "Failed to parse XML" )
57+ }
58+
2959/// Firmware type for virtual machines
3060#[ derive( Debug , Clone , Copy , PartialEq , Eq , ValueEnum ) ]
3161#[ clap( rename_all = "kebab-case" ) ]
@@ -343,24 +373,8 @@ pub fn get_libvirt_storage_pool_path(connect_uri: Option<&str>) -> Result<Utf8Pa
343373 // Ensure pool exists before querying
344374 ensure_default_pool ( connect_uri) ?;
345375
346- let mut cmd = virsh_command ( connect_uri) ?;
347- cmd. args ( & [ "pool-dumpxml" , "default" ] ) ;
348- let output = cmd
349- . output ( )
350- . with_context ( || "Failed to query libvirt storage pool" ) ?;
351-
352- if !output. status . success ( ) {
353- let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
354- let uri_desc = connect_uri. unwrap_or ( "default connection" ) ;
355- return Err ( color_eyre:: eyre:: eyre!(
356- "Failed to get default storage pool info for {}: {}" ,
357- uri_desc,
358- stderr
359- ) ) ;
360- }
361-
362- let xml = String :: from_utf8 ( output. stdout ) . with_context ( || "Invalid UTF-8 in virsh output" ) ?;
363- let dom = xml_utils:: parse_xml_dom ( & xml) . with_context ( || "Failed to parse storage pool XML" ) ?;
376+ let dom = run_virsh_xml ( connect_uri, & [ "pool-dumpxml" , "default" ] )
377+ . context ( "Failed to get default storage pool info" ) ?;
364378
365379 if let Some ( path_node) = dom. find ( "path" ) {
366380 let path_str = path_node. text_content ( ) . trim ( ) ;
0 commit comments