diff --git a/src/utils.rs b/src/utils.rs index 601c042..b4ccc7f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,15 +1,26 @@ use std::fs; +use std::fs::File; +use std::io::ErrorKind; use std::time::Duration; use serde::de::DeserializeOwned; +use tap::Pipe; pub mod logs; pub mod booking; pub mod index; pub fn parse_toml_file(path: &str) -> T { - let file_content = fs::read_to_string(path).expect("failed to read .toml file"); - toml::from_str(&file_content).expect("failed to parse toml") + match fs::read_to_string(path) { + Ok(s) => s, + Err(e) if e.kind() == ErrorKind::NotFound => { + File::create(path).expect("failed to create file"); + String::new() + }, + _ => panic!("error reading {path}") + } + .pipe_as_ref(toml::from_str) + .expect("failed to parse toml") } pub fn get_power_state(payload: &str) -> Result { diff --git a/src/utils/logs.rs b/src/utils/logs.rs index de1b7c4..202719e 100644 --- a/src/utils/logs.rs +++ b/src/utils/logs.rs @@ -32,6 +32,7 @@ pub fn machinelog(machine: &str, booking: &Booking) -> io::Result<()> { }; let file_writer = File::options() + .create(true) .append(true) .open("/root/machinelog.csv")?; @@ -47,6 +48,7 @@ pub fn machinelog(machine: &str, booking: &Booking) -> io::Result<()> { pub fn log_start() -> io::Result<()> { File::options() + .create(true) .append(true) .open("/root/machinelog_debug.csv")? .write_all(format!("\n\n===== startup {} =====\n\n", Local::now()).as_bytes())