diff options
Diffstat (limited to 'src/cmd/list.rs')
-rw-r--r-- | src/cmd/list.rs | 43 |
1 files changed, 7 insertions, 36 deletions
diff --git a/src/cmd/list.rs b/src/cmd/list.rs index c83be26..40a1910 100644 --- a/src/cmd/list.rs +++ b/src/cmd/list.rs @@ -1,45 +1,16 @@ //! The `list` sub-command. use crate::config::Configuration; - -use virt::connect::Connect; - -/// Errors returned from this module. -#[derive(Debug, thiserror::Error)] -pub enum ListError { - /// An error from libvirt. - #[error(transparent)] - VirtError(#[from] virt::error::Error), -} +use crate::libvirt::{Libvirt, VirtError}; /// The `list` sub-command. /// -/// Return all the virtual machines existing on the libvirt instance, -/// and their current state. -pub fn list(_config: &Configuration) -> Result<(), ListError> { - let conn = Connect::open("qemu:///system")?; - let domains = conn.list_all_domains(0)?; - for domain in domains { - let name = domain.get_name()?; - let (state, _) = domain.get_state()?; - let state = state_name(state); - println!("{} {}", name, state); +/// Return the names of all the virtual machines existing on the +/// libvirt instance. +pub fn list(_config: &Configuration) -> Result<(), VirtError> { + let libvirt = Libvirt::connect("qemu:///system")?; + for name in libvirt.names()? { + println!("{}", name); } - Ok(()) } - -fn state_name(state: virt::domain::DomainState) -> String { - let name = match state { - virt::domain::VIR_DOMAIN_NOSTATE => "none", - virt::domain::VIR_DOMAIN_RUNNING => "running", - virt::domain::VIR_DOMAIN_BLOCKED => "blocked", - virt::domain::VIR_DOMAIN_PAUSED => "paused", - virt::domain::VIR_DOMAIN_SHUTDOWN => "shutdown", - virt::domain::VIR_DOMAIN_SHUTOFF => "shutoff", - virt::domain::VIR_DOMAIN_CRASHED => "crashed", - virt::domain::VIR_DOMAIN_PMSUSPENDED => "power management suspended", - _ => "unknown", - }; - name.to_string() -} |