use config::{Config, ConfigError, Environment, File, FileFormat};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct SchedulerSettings {
pub url: String,
}
#[derive(Debug, Deserialize)]
pub struct HttpSettings {
pub timeout: u64,
}
#[derive(Debug, Deserialize)]
pub struct JobSettings {
pub stage: JobStageSettings,
}
#[derive(Debug, Deserialize)]
pub struct JobStageSettings {
pub plan: JobPlanSettings,
}
#[derive(Debug, Deserialize)]
pub struct JobPlanSettings {
pub tree: bool,
}
#[derive(Debug, Deserialize)]
pub struct Settings {
pub scheduler: SchedulerSettings,
pub http: HttpSettings,
pub tick_interval_ms: u64,
pub job: JobSettings,
}
const DEFAULT_CONFIG: &str = r#"
tick_interval_ms: 2000
scheduler:
url: http://localhost:50050
http:
timeout: 2000
job:
stage:
plan:
tree: false
"#;
impl Settings {
pub(crate) fn new() -> Result<Self, ConfigError> {
let config_dir = dirs::config_local_dir()
.or_else(|| dirs::home_dir().map(|h| h.join(".config")))
.unwrap_or_else(|| std::path::PathBuf::from(".config"))
.join("ballista");
let s = Config::builder()
.add_source(File::from_str(DEFAULT_CONFIG, FileFormat::Yaml))
.add_source(
File::with_name(&format!("{}/tui", config_dir.display()))
.format(FileFormat::Yaml)
.required(false),
)
.add_source(Environment::with_prefix("BALLISTA").separator("_"))
.build()?;
s.try_deserialize()
}
}