use anyhow::Result;
use std::path::{Path, PathBuf};
use crate::Config;
use super::loader::MultiFileLoader;
pub struct ConfigDirectory {
root: PathBuf,
}
impl ConfigDirectory {
pub fn new(root: impl AsRef<Path>) -> Self {
Self {
root: root.as_ref().to_path_buf(),
}
}
pub fn load(&self, environment: Option<&str>) -> Result<Config> {
let mut loader = MultiFileLoader::new(&self.root);
if self.root.join("zentinel.kdl").exists() {
loader = loader.with_include("zentinel.kdl");
}
let subdirs = ["listeners", "routes", "upstreams", "agents"];
for subdir in subdirs {
let dir = self.root.join(subdir);
if dir.exists() {
loader = loader.with_include(format!("{}/*.kdl", subdir));
}
}
if let Some(env) = environment {
let env_file = format!("environments/{}.kdl", env);
let env_path = self.root.join(&env_file);
if env_path.exists() {
loader = loader.with_include(env_file);
}
}
loader = loader
.with_exclude("*.example.kdl")
.with_exclude("*.bak")
.with_exclude("*~");
loader.load()
}
}