diff options
author | Alyssa Ross <hi@alyssa.is> | 2022-04-27 19:16:47 +0000 |
---|---|---|
committer | Alyssa Ross <hi@alyssa.is> | 2022-04-27 21:11:09 +0000 |
commit | ab9d7bf6d7e7b4cdd77933d57992aad64be3c4f6 (patch) | |
tree | ea5683bce3fc9b1bdabbf5199d9f8cf699c3c370 | |
parent | 396c734a3606cee35504f55df6a35949cf19f177 (diff) | |
download | spectrum-ab9d7bf6d7e7b4cdd77933d57992aad64be3c4f6.tar spectrum-ab9d7bf6d7e7b4cdd77933d57992aad64be3c4f6.tar.gz spectrum-ab9d7bf6d7e7b4cdd77933d57992aad64be3c4f6.tar.bz2 spectrum-ab9d7bf6d7e7b4cdd77933d57992aad64be3c4f6.tar.lz spectrum-ab9d7bf6d7e7b4cdd77933d57992aad64be3c4f6.tar.xz spectrum-ab9d7bf6d7e7b4cdd77933d57992aad64be3c4f6.tar.zst spectrum-ab9d7bf6d7e7b4cdd77933d57992aad64be3c4f6.zip |
host/start-vm: use returns instead of early exits
I thought that returning early would make the code more noisy, but with String as the error type, it doesn't!
-rw-r--r-- | host/start-vm/start-vm.rs | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/host/start-vm/start-vm.rs b/host/start-vm/start-vm.rs index 0d08eaa..484fd60 100644 --- a/host/start-vm/start-vm.rs +++ b/host/start-vm/start-vm.rs @@ -13,21 +13,14 @@ use std::process::{exit, Command}; use net::{format_mac, net_setup, NetConfig}; -macro_rules! errx { - ($code:expr, $fmt:expr $(,$args:expr)*) => ({ - let argv0_option = args().next(); - let argv0 = argv0_option.as_ref().map(String::as_str).unwrap_or("start-vm"); - eprintln!(concat!("{}: ", $fmt), argv0 $(,$args)*); - exit($code); - }) -} +fn vm_command(dir: PathBuf) -> Result<Command, String> { + let dir = dir.into_os_string().into_vec(); + let dir = PathBuf::from(OsString::from_vec(dir)); -macro_rules! err { - ($code:expr, $fmt:expr $(,$args:expr)*) => - (|e| errx!($code, concat!($fmt, ": {}") $(,$args)*, e)) -} + let vm_name = dir + .file_name() + .ok_or_else(|| "directory has no name".to_string())?; -fn main() { let mut command = Command::new("s6-notifyoncheck"); command.args(&["-dc", "test -S env/cloud-hypervisor.sock"]); command.arg("cloud-hypervisor"); @@ -36,16 +29,6 @@ fn main() { command.args(&["--memory", "size=128M"]); command.args(&["--console", "pty"]); - let dir = current_dir() - .unwrap_or_else(err!(1, "getting current directory")) - .into_os_string() - .into_vec(); - let dir = PathBuf::from(OsString::from_vec(dir)); - - let vm_name = dir - .file_name() - .unwrap_or_else(|| errx!(1, "current directory has no name")); - let mut net_providers_dir = PathBuf::new(); net_providers_dir.push("/ext/svc/data"); net_providers_dir.push(vm_name); @@ -55,7 +38,7 @@ fn main() { Ok(entries) => { for r in entries { let entry = r - .unwrap_or_else(err!(1, "examining directory entry")) + .map_err(|e| format!("examining directory entry: {}", e))? .file_name(); // Safe because prov is the name of a directory entry, so @@ -66,7 +49,7 @@ fn main() { let NetConfig { fd, mac } = unsafe { net_setup(provider_name.as_ptr()) }; if fd == -1 { let e = io::Error::last_os_error(); - errx!(1, "setting up networking failed: {}", e); + return Err(format!("setting up networking failed: {}", e)); } command @@ -79,7 +62,7 @@ fn main() { } } Err(e) if e.kind() == ErrorKind::NotFound => {} - Err(e) => errx!(1, "reading directory {:?}: {}", net_providers_dir, e), + Err(e) => return Err(format!("reading directory {:?}: {}", net_providers_dir, e)), } command.arg("--kernel").arg({ @@ -103,5 +86,27 @@ fn main() { serial }); - errx!(1, "failed to exec: {}", command.exec()); + Ok(command) +} + +fn run() -> String { + let dir = match current_dir().map_err(|e| format!("getting current directory: {}", e)) { + Ok(dir) => dir, + Err(e) => return e, + }; + + match vm_command(dir) { + Ok(mut command) => format!("failed to exec: {}", command.exec()), + Err(e) => e, + } +} + +fn main() { + let argv0_option = args().next(); + let argv0 = argv0_option + .as_ref() + .map(String::as_str) + .unwrap_or("start-vm"); + eprintln!("{}: {}", argv0, run()); + exit(1); } |