Skip to main content

dotm/
loader.rs

1use crate::config::{HostConfig, RoleConfig, RootConfig};
2use anyhow::{Context, Result, bail};
3use std::path::{Path, PathBuf};
4
5pub struct ConfigLoader {
6    base_dir: PathBuf,
7    root: RootConfig,
8}
9
10impl ConfigLoader {
11    pub fn new(base_dir: &Path) -> Result<Self> {
12        let config_path = base_dir.join("dotm.toml");
13        let content = std::fs::read_to_string(&config_path)
14            .with_context(|| format!("failed to read {}", config_path.display()))?;
15        let root: RootConfig = toml::from_str(&content)
16            .with_context(|| format!("failed to parse {}", config_path.display()))?;
17
18        Ok(Self {
19            base_dir: base_dir.to_path_buf(),
20            root,
21        })
22    }
23
24    pub fn root(&self) -> &RootConfig {
25        &self.root
26    }
27
28    pub fn base_dir(&self) -> &Path {
29        &self.base_dir
30    }
31
32    pub fn packages_dir(&self) -> PathBuf {
33        self.base_dir.join(&self.root.dotm.packages_dir)
34    }
35
36    pub fn load_host(&self, hostname: &str) -> Result<HostConfig> {
37        let path = self.base_dir.join("hosts").join(format!("{hostname}.toml"));
38        if !path.exists() {
39            bail!("host config not found: {}", path.display());
40        }
41        let content = std::fs::read_to_string(&path)
42            .with_context(|| format!("failed to read {}", path.display()))?;
43        let config: HostConfig = toml::from_str(&content)
44            .with_context(|| format!("failed to parse {}", path.display()))?;
45        Ok(config)
46    }
47
48    pub fn load_role(&self, name: &str) -> Result<RoleConfig> {
49        let path = self.base_dir.join("roles").join(format!("{name}.toml"));
50        if !path.exists() {
51            bail!("role config not found: {}", path.display());
52        }
53        let content = std::fs::read_to_string(&path)
54            .with_context(|| format!("failed to read {}", path.display()))?;
55        let config: RoleConfig = toml::from_str(&content)
56            .with_context(|| format!("failed to parse {}", path.display()))?;
57        Ok(config)
58    }
59}