use std::path::PathBuf;
use anyhow::{anyhow, Result};
pub fn load_service_template(path: &str) -> Result<String> {
if let Ok(template_dir) = std::env::var("TONIN_TEMPLATE_DIR") {
let full_path = PathBuf::from(&template_dir)
.join("service")
.join(path);
if full_path.exists() {
if let Ok(contents) = std::fs::read_to_string(&full_path) {
return Ok(contents);
}
}
}
Err(anyhow!(
"Template not found: {} (set TONIN_TEMPLATE_DIR to override)",
path
))
}
pub fn list_service_templates(rel_path: &str) -> Result<Vec<String>> {
if let Ok(template_dir) = std::env::var("TONIN_TEMPLATE_DIR") {
let dir_path = PathBuf::from(&template_dir)
.join("service")
.join(rel_path);
if dir_path.exists() && dir_path.is_dir() {
let mut files = Vec::new();
for entry in std::fs::read_dir(&dir_path)? {
if let Ok(entry) = entry {
if let Ok(file_name) = entry.file_name().into_string() {
files.push(file_name);
}
}
}
return Ok(files);
}
}
Err(anyhow!(
"Template directory not found: {} (set TONIN_TEMPLATE_DIR to override)",
rel_path
))
}
pub fn service_template_exists(path: &str) -> bool {
if let Ok(template_dir) = std::env::var("TONIN_TEMPLATE_DIR") {
let full_path = PathBuf::from(&template_dir)
.join("service")
.join(path);
if full_path.exists() {
return true;
}
}
false
}