use super::{Call, CliError, argv};
use crate::process::Captured;
const TOOL: &str = "utmctl";
pub(crate) async fn name_of(call: &Call<'_>, uuid: &str) -> Result<Option<String>, CliError<String>> {
let out = call.ok(TOOL, argv(["list"]), Captured::reason).await?;
Ok(named(&out.stdout, uuid))
}
fn named(lines: &[String], uuid: &str) -> Option<String> {
lines.iter().find_map(|line| {
let rest = line.trim_start().strip_prefix(uuid)?;
let (_status, name) = rest.trim_start().split_once(char::is_whitespace)?;
Some(name.trim().to_owned())
})
}
const NO_VM: [&str; 2] = ["virtual machine not found", "no virtual machine"];
pub(crate) async fn remove(call: &Call<'_>, uuid: &str) -> Result<(), CliError<String>> {
super::stop_then_delete(call, TOOL, argv(["stop", "--kill", uuid]), argv(["delete", uuid]), &NO_VM).await
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_vm_is_named_by_its_line() {
let lines: Vec<String> = [
"UUID Status Name",
"AB-12 started web vm",
"CD-34 stopped db",
]
.map(str::to_owned)
.to_vec();
assert_eq!(named(&lines, "AB-12").as_deref(), Some("web vm"));
assert_eq!(named(&lines, "EF-56"), None);
}
}