aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.rs
blob: 173b8ffaced54d64b349d6aae87bb969bb19ab0b (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 port: Option<u32>,
    pub dns: Option<String>,
    pub wgconf: Option<String>,
}

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

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