use home::home_dir;
use crate::BONNIE_VERSION;
use std::env;
use std::fs;
use std::path::PathBuf;
fn get_inbuilt_default_template() -> String {
format!(
"version=\"{version}\"
[scripts]
start = \"echo \\\"No start script yet!\\\"\"",
version = BONNIE_VERSION
)
}
fn get_template_path() -> Option<PathBuf> {
match env::var("BONNIE_TEMPLATE") {
Ok(path) => Some(PathBuf::from(path)),
Err(_) => {
home_dir()
.map(|path| path.join(".bonnie").join("template.toml"))
.map(|path| if path.exists() { Some(path) } else { None })
.flatten()
}
}
}
pub fn get_default_template() -> Result<String, String> {
let path = get_template_path();
if let Some(path) = path {
let template = fs::read_to_string(path);
match template {
Ok(template) => Ok(template),
Err(err) => Err(format!("Failed to get default template file. Please make sure any path in 'BONNIE_TEMPLATE' definitely exists. Error was '{}'.", err))
}
} else {
Ok(get_inbuilt_default_template())
}
}