fix: use newly created macros to handle errors from quickemu

This commit is contained in:
Oskar Manhart 2023-10-08 00:43:21 +02:00
parent 8f30ea0e83
commit 5087c62795

View File

@ -1,29 +1,25 @@
use crate::{get_data_dir, save_config, Config}; use crate::{get_data_dir, save_config, unwrap_or_exit, Config};
use std::fs; use std::fs;
use std::process::exit;
use std::process::Command; use std::process::Command;
use tracing::info;
pub fn create_vm(mut config: Config) { pub fn create_vm(mut config: Config) {
let data_dir = get_data_dir(); let data_dir = get_data_dir();
let output = match Command::new("quickget") let output = unwrap_or_exit!(
.current_dir(data_dir) Command::new("quickget")
.arg("windows") .current_dir(data_dir)
.arg("10") .arg("windows")
.output() .arg("10")
{ .output(),
Ok(o) => o, "Failed to execute quickget: \n\
Err(e) => { Please make sure quickget is installed and in your PATH"
println!("Failed to execute quickget: {}", e); );
println!("Please make sure quickget is installed and in your PATH");
exit(1);
}
};
config.vm.name = "windows-10-22H2".to_string(); config.vm.name = "windows-10-22H2".to_string();
config.vm.short_name = "windows-10".to_string(); config.vm.short_name = "windows-10".to_string();
save_config(&config, None).expect("Failed to save config, VM will not start properly"); unwrap_or_exit!(save_config(&config, None), "Failed to save config");
println!("{}", String::from_utf8_lossy(&output.stdout)); println!("{}", String::from_utf8_lossy(&output.stdout));
} }
@ -31,32 +27,24 @@ pub fn create_vm(mut config: Config) {
pub fn start_vm(config: Config) { pub fn start_vm(config: Config) {
let data_dir = get_data_dir(); let data_dir = get_data_dir();
let command = match Command::new("quickemu") let command = unwrap_or_exit!(
.current_dir(data_dir) Command::new("quickemu")
.args([ .current_dir(data_dir)
"--vm", .args([
&format!("{}.conf", config.vm.name), "--vm",
"--display", &format!("{}.conf", config.vm.name),
"none", "--display",
]) "none",
.spawn() ])
{ .spawn(),
Ok(c) => c, "Failed to execute quickemu: \n\
Err(e) => { Please make sure quickemu is installed and in your PATH"
println!("Failed to execute quickemu: {}", e); );
println!("Please make sure quickemu is installed and in your PATH");
exit(1);
}
};
let output = match command.wait_with_output() { let output = unwrap_or_exit!(
Ok(o) => o, command.wait_with_output(),
Err(e) => { "Failed to gather output from quickemu / stdout"
println!("Failed to gather output from quickemu: {}", e); );
println!("Please make sure quickemu is installed and in your PATH");
exit(1);
}
};
println!("{}", String::from_utf8_lossy(&output.stdout)); println!("{}", String::from_utf8_lossy(&output.stdout));
} }
@ -64,25 +52,17 @@ pub fn start_vm(config: Config) {
pub fn kill_vm(config: Config) { pub fn kill_vm(config: Config) {
let data_dir = get_data_dir(); let data_dir = get_data_dir();
match fs::read_to_string( let pid = unwrap_or_exit!(
data_dir.join(format!("{}/{}.pid", config.vm.short_name, config.vm.name)), fs::read_to_string(
) { data_dir.join(format!("{}/{}.pid", config.vm.short_name, config.vm.name)),
Ok(pid) => { ),
let pid = pid.trim(); "Failed to read PID file, is the VM running and the config correct?"
);
println!("Killing VM with PID {}", pid); info!("Killing VM with PID {}", pid);
match Command::new("kill").arg(pid).spawn() { unwrap_or_exit!(
Ok(_) => (), Command::new("kill").arg(pid).spawn(),
Err(e) => { "Failed to kill VM (execute kill)"
println!("Failed to kill VM: {}", e); );
exit(1);
}
}
}
Err(e) => {
println!("Failed to read PID file: {}", e);
exit(1);
}
}
} }