Skip to main content

salix_config/
lib.rs

1//! Salix config
2
3use std::{env::current_dir, net::SocketAddr, path::PathBuf};
4
5use anyhow::Result;
6use serde::Deserialize;
7
8#[derive(Deserialize, Clone, Debug)]
9pub struct ControllerConfig {
10    pub listen: SocketAddr,
11    pub cert_path: PathBuf,
12    pub key_path: PathBuf,
13}
14
15#[derive(Deserialize, Clone, Debug)]
16pub struct AgentConfig {
17    pub controller_address: String,
18    pub cert_path: PathBuf,
19}
20
21#[derive(Deserialize, Clone, Debug)]
22pub struct Config {
23    pub controller: ControllerConfig,
24    pub agent: AgentConfig,
25}
26
27pub fn get_config(config_path: Option<PathBuf>) -> Result<Config> {
28    let config_path = if let Some(config_path) = config_path {
29        config_path
30    } else {
31        let mut config_path = current_dir()?;
32        config_path.push("salix.toml");
33        config_path
34    };
35
36    // TODO: Add env source
37    let settings = config::Config::builder()
38        .add_source(config::File::from(config_path))
39        .build()?;
40    Ok(settings.try_deserialize()?)
41}