@@ -41,6 +41,36 @@ pub(crate) fn run_virsh_cmd(connect_uri: Option<&str>, args: &[&str], err_msg: &
4141 Ok ( ( ) )
4242}
4343
44+ /// Run a virsh command that returns XML and parse it directly
45+ ///
46+ /// This helper function consolidates the common pattern of:
47+ /// 1. Running a virsh command with arguments
48+ /// 2. Checking if the command succeeded
49+ /// 3. Parsing the XML output from the stdout bytes
50+ ///
51+ /// # Arguments
52+ /// * `connect_uri` - Optional libvirt connection URI
53+ /// * `args` - Arguments to pass to virsh
54+ ///
55+ /// # Returns
56+ /// The parsed XML as an XmlNode
57+ pub fn run_virsh_xml ( connect_uri : Option < & str > , args : & [ & str ] ) -> Result < xml_utils:: XmlNode > {
58+ let mut cmd = virsh_command ( connect_uri) ?;
59+ cmd. args ( args) ;
60+
61+ let output = cmd. output ( ) . context ( "Failed to run virsh" ) ?;
62+
63+ if !output. status . success ( ) {
64+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
65+ return Err ( eyre:: eyre!( "virsh command failed: {}" , stderr) ) ;
66+ }
67+
68+ // Parse XML directly from bytes
69+ let xml = std:: str:: from_utf8 ( & output. stdout ) . context ( "Invalid UTF-8 in virsh output" ) ?;
70+
71+ xml_utils:: parse_xml_dom ( xml) . context ( "Failed to parse XML" )
72+ }
73+
4474/// Firmware type for virtual machines
4575#[ derive( Debug , Clone , Copy , PartialEq , Eq , ValueEnum ) ]
4676#[ clap( rename_all = "kebab-case" ) ]
@@ -525,24 +555,8 @@ pub fn get_libvirt_storage_pool_path(connect_uri: Option<&str>) -> Result<Utf8Pa
525555 // Ensure pool exists before querying
526556 ensure_default_pool ( connect_uri) ?;
527557
528- let mut cmd = virsh_command ( connect_uri) ?;
529- cmd. args ( & [ "pool-dumpxml" , "default" ] ) ;
530- let output = cmd
531- . output ( )
532- . with_context ( || "Failed to query libvirt storage pool" ) ?;
533-
534- if !output. status . success ( ) {
535- let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
536- let uri_desc = connect_uri. unwrap_or ( "default connection" ) ;
537- return Err ( color_eyre:: eyre:: eyre!(
538- "Failed to get default storage pool info for {}: {}" ,
539- uri_desc,
540- stderr
541- ) ) ;
542- }
543-
544- let xml = String :: from_utf8 ( output. stdout ) . with_context ( || "Invalid UTF-8 in virsh output" ) ?;
545- let dom = xml_utils:: parse_xml_dom ( & xml) . with_context ( || "Failed to parse storage pool XML" ) ?;
558+ let dom = run_virsh_xml ( connect_uri, & [ "pool-dumpxml" , "default" ] )
559+ . context ( "Failed to get default storage pool info" ) ?;
546560
547561 if let Some ( path_node) = dom. find ( "path" ) {
548562 let path_str = path_node. text_content ( ) . trim ( ) ;
0 commit comments