diff --git a/src/file_utils.rs b/src/file_utils.rs new file mode 100644 index 0000000..3a41ef2 --- /dev/null +++ b/src/file_utils.rs @@ -0,0 +1,40 @@ +use std::{ + fs::{create_dir_all, OpenOptions}, + io::BufWriter, + path::Path, env, +}; + +use crate::constants::CMD_NAME; + +pub fn get_writer(path_s: &String) -> BufWriter { + let path = Path::new(path_s); + match path.parent() { + Some(parent) => { + if !parent.is_dir() { + create_dir_all(parent).expect("Could not create dir") + } + } + None => {} + }; + let file = OpenOptions::new() + .write(true) + .create(true) + .open(path) + .expect("Could not open file"); + BufWriter::new(file) +} + +pub fn get_config_dir() -> String { + match env::var("XDG_CONFIG_HOME") { + Ok(conf_home) => format!( + "{chome}/{name}", + chome = conf_home, + name = CMD_NAME + ), + Err(_) => format!( + "{home}/.config/{name}", + home = env::var("HOME").expect("HOME env var not defined"), + name = CMD_NAME + ), + } +} diff --git a/src/profile.rs b/src/profile.rs index e463517..b2b3408 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -3,10 +3,12 @@ use std::{ collections::HashMap, fs::{create_dir_all, File, OpenOptions}, io::{BufReader, BufWriter}, - path::Path, + path::Path, fmt::Display, }; -#[derive(Debug, Clone, Serialize, Deserialize)] +use crate::file_utils::get_writer; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Profile { pub name: String, pub monado_path: String, @@ -20,6 +22,12 @@ pub struct Profile { pub environment: HashMap, } +impl Display for Profile { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.name.to_string()) + } +} + pub fn load_profile(path: &String) -> Result { let file = File::open(path).unwrap(); let reader = BufReader::new(file); @@ -27,21 +35,7 @@ pub fn load_profile(path: &String) -> Result { } pub fn dump_profile(profile: Profile, path_s: &String) -> () { - let path = Path::new(path_s); - match path.parent() { - Some(parent) => { - if !parent.is_dir() { - create_dir_all(parent).expect("Could not create dir") - } - } - None => {} - }; - let file = OpenOptions::new() - .write(true) - .create(true) - .open(path) - .expect("Could not open file"); - let writer = BufWriter::new(file); + let writer = get_writer(path_s); serde_json::to_writer_pretty(writer, &profile).expect("Could not write profile") } diff --git a/src/runner.rs b/src/runner.rs index 2097509..4a25da6 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -6,7 +6,7 @@ use std::{ process::{Child, Command, Stdio}, }; -use crate::profile::Profile; +use crate::{profile::Profile, file_utils::get_writer}; pub struct Runner { environment: HashMap, @@ -118,21 +118,7 @@ impl Runner { } fn save_log(path_s: String, log: &Vec) -> Result<(), std::io::Error> { - let path = Path::new(&path_s); - match path.parent() { - Some(parent) => { - if !parent.is_dir() { - create_dir_all(parent).expect("Could not create dir") - } - } - None => {} - }; - let file = OpenOptions::new() - .write(true) - .create(true) - .open(path) - .expect("Could not open file"); - let mut writer = BufWriter::new(file); + let mut writer = get_writer(&path_s); let log_s = log.concat(); writer.write_all(log_s.as_ref()) }