Mercurial > hg > Members > anatofuz > ie-virsh
changeset 16:1da4937628be
tweak
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Sun, 01 Nov 2020 14:02:35 +0900 |
parents | e6dc51066703 |
children | 5477c26e6984 |
files | src/command.rs src/virsh.rs |
diffstat | 2 files changed, 11 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/src/command.rs Sun Nov 01 12:52:13 2020 +0900 +++ b/src/command.rs Sun Nov 01 14:02:35 2020 +0900 @@ -10,15 +10,17 @@ } pub fn start_command(user_name: &'static str, vm_name: String) { + virsh::command_require_vm_name(get_vm_name(user_name, vm_name), "start"); +} + +fn get_vm_name(user_name: &'static str, vm_name: String) -> String { let vm_name = match vm_name.parse::<u8>() { Ok(_) => format!("{}-{}", user_name, vm_name), Err(_) => vm_name, }; - let vm_name = if vm_name.starts_with(user_name) { - vm_name - } else { - format!("{}-{}", user_name, vm_name) - }; - virsh::start_vm(vm_name); + if vm_name.starts_with(user_name) { + return vm_name; + } + format!("{}-{}", user_name, vm_name) }
--- a/src/virsh.rs Sun Nov 01 12:52:13 2020 +0900 +++ b/src/virsh.rs Sun Nov 01 14:02:35 2020 +0900 @@ -31,12 +31,12 @@ ) } -pub fn start_vm(vm_name: String) { +pub fn command_require_vm_name(vm_name: String, operation: &str) { let output = Command::new("virsh") - .arg("start") + .arg(operation) .arg(vm_name) .output() - .expect("failed to start vm"); + .unwrap_or_else(|_| panic!("failed to {}", operation)); io::stdout().write_all(&output.stdout).unwrap(); io::stderr().write_all(&output.stderr).unwrap();