summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: a13c989ec31d48acba3414f1344cff612a4af3f1 (plain)
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
use log::debug;
use serde::Deserialize;
use std::default::Default;
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Default, Debug, Deserialize)]
pub struct Configuration {
    pub default_base_image: Option<PathBuf>,
    pub default_image_gib: Option<u64>,
    pub default_memory_mib: Option<u64>,
    pub default_cpus: Option<u64>,
    pub image_directory: Option<PathBuf>,
    pub authorized_keys: Option<Vec<PathBuf>>,
}

#[derive(Debug, thiserror::Error)]
pub enum ConfigurationError {
    #[error(transparent)]
    IoError(#[from] std::io::Error),

    #[error(transparent)]
    YamlError(#[from] serde_yaml::Error),
}

impl Configuration {
    pub fn from_file(filename: &Path) -> Result<Self, ConfigurationError> {
        if filename.exists() {
            debug!("reading configuration file {}", filename.display());
            let config = fs::read(filename)?;
            let config: Configuration = serde_yaml::from_slice(&config)?;
            debug!("config: {:#?}", config);
            Ok(config)
        } else {
            Ok(Self::default())
        }
    }
}