summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 46d220425a91b205def64c23aea05eb8656ea9e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::{fs::create_dir_all, io::Write, path::PathBuf};

use config::{File, Map};
use serde::{Deserialize, Serialize};
use tracing::{debug, trace};

#[derive(Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
    pub root: String,
    pub directories: Vec<String>,
    pub custom_directories: Map<String, String>,
    pub device: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            root: "/mnt/backup".to_string(),
            directories: vec![],
            custom_directories: Map::new(),
            device: gethostname::gethostname()
                .into_string()
                .expect("invalid hostname string"),
        }
    }
}

impl Config {
    pub fn load(path: Option<PathBuf>) -> core::result::Result<Self, config::ConfigError> {
        debug!("load config");
        let source = if let Some(source) = path {
            source
        } else {
            Self::get_location()
        };

        let config = config::Config::builder()
            .add_source(File::with_name(&source.to_string_lossy()).required(false))
            .add_source(config::Environment::with_prefix("FXBAUP").separator("_"))
            .build()?;

        let cfg = config.try_deserialize();
        trace!(?cfg, "loaded config");

        cfg
    }

    pub fn generate() -> crate::error::Result<()> {
        let loc = Self::get_location();
        create_dir_all(loc.parent().unwrap())?;
        let mut f = std::fs::File::create(loc)?;
        f.write_all(toml::to_string(&Self::default())?.as_bytes())?;

        Ok(())
    }

    fn get_location() -> PathBuf {
        let mut conf_dir = dirs::config_local_dir().unwrap();
        conf_dir.push("arbs");
        conf_dir.push("config.toml");
        conf_dir
    }
}