use dirs;
use std::{env, io, path::PathBuf};
pub const TGT: &str = "tgt";
pub const TGT_CONFIG_DIR: &str = "TGT_CONFIG_DIR";
pub fn tgt_dir() -> io::Result<PathBuf> {
if cfg!(debug_assertions) {
return env::current_dir();
}
let home = dirs::home_dir().unwrap().to_str().unwrap().to_owned();
let tgt = format!("{}/.tgt", home);
if PathBuf::from(&tgt).exists() {
Ok(PathBuf::from(&tgt))
} else {
panic!("The directory {} does not exist.", tgt);
}
}
pub fn tgt_config_dir() -> io::Result<PathBuf> {
Ok(tgt_dir()?.join("config"))
}
fn fail_with<E: std::fmt::Debug>(msg: &str, e: E) -> ! {
eprintln!("[ERROR]: {} {:?}", msg, e);
std::process::exit(1);
}
pub fn unwrap_or_fail<T, E: std::fmt::Debug>(result: Result<T, E>, msg: &str) -> T {
match result {
Ok(v) => v,
Err(e) => fail_with(msg, e),
}
}