aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.rs
blob: 839db24ca233f24d104dc7c70f1d3af3b7dbf2c7 (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
use serde::Deserialize;
use anyhow::{Result, anyhow, Context};
use std::fs::read_to_string;

#[derive(Deserialize)]
pub struct Configuration {
    pub endpoint: String,
    pub wgconf: String,
    pub port: Option<u32>,
    pub dns: Option<String>,
}

pub fn find_configuration_file(argument: Option<&String>) -> Result<Configuration> {
    match argument {
        Some(p) => {
            if let Ok(t) = read_to_string(p) {
                let c: Configuration = toml::from_str(&t).context("parsing configuration file")?;
                return Ok(c)
            }
        },
        None => {
            // Try /etc/wgmgr.toml
            if let Ok(t) = read_to_string("/etc/wgmgr.toml") {
                let c: Configuration = toml::from_str(&t).context("parsing /etc/wgmgr.toml")?;
                return Ok(c)
            }
        },
    };

    Err(anyhow!("Could not find a valid configuration file for wgmgr."))
}