use std::{env::current_dir, net::SocketAddr, path::PathBuf};
use anyhow::Result;
use serde::Deserialize;
#[derive(Deserialize, Clone, Debug)]
pub struct ControllerConfig {
pub listen: SocketAddr,
pub cert_path: PathBuf,
pub key_path: PathBuf,
}
#[derive(Deserialize, Clone, Debug)]
pub struct AgentConfig {
pub controller_address: String,
pub cert_path: PathBuf,
}
#[derive(Deserialize, Clone, Debug)]
pub struct Config {
pub controller: ControllerConfig,
pub agent: AgentConfig,
}
pub fn get_config(config_path: Option<PathBuf>) -> Result<Config> {
let config_path = if let Some(config_path) = config_path {
config_path
} else {
let mut config_path = current_dir()?;
config_path.push("salix.toml");
config_path
};
let settings = config::Config::builder()
.add_source(config::File::from(config_path))
.build()?;
Ok(settings.try_deserialize()?)
}