| 1 |
use anyhow::{Context, Result}; |
| 2 |
use serde::Deserialize; |
| 3 |
use std::path::PathBuf; |
| 4 |
|
| 5 |
#[derive(Debug, Clone, Deserialize)] |
| 6 |
pub struct Config { |
| 7 |
pub listen: String, |
| 8 |
pub db_path: PathBuf, |
| 9 |
pub topology_path: PathBuf, |
| 10 |
|
| 11 |
pub workdir: PathBuf, |
| 12 |
|
| 13 |
pub release_root: PathBuf, |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#[serde(default)] |
| 18 |
pub scratch_db_url: Option<String>, |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#[serde(default = "default_bin_names")] |
| 24 |
pub bin_names: Vec<String>, |
| 25 |
|
| 26 |
|
| 27 |
#[serde(default = "default_logs_root")] |
| 28 |
pub logs_root: PathBuf, |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#[serde(default)] |
| 36 |
pub release_contents: Vec<ReleaseEntry>, |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#[derive(Debug, Clone, Deserialize)] |
| 43 |
pub struct ReleaseEntry { |
| 44 |
|
| 45 |
pub src: PathBuf, |
| 46 |
|
| 47 |
|
| 48 |
pub dst: PathBuf, |
| 49 |
|
| 50 |
#[serde(default)] |
| 51 |
pub required: bool, |
| 52 |
} |
| 53 |
|
| 54 |
fn default_bin_names() -> Vec<String> { vec!["server".into()] } |
| 55 |
fn default_logs_root() -> PathBuf { PathBuf::from("/srv/sando/logs") } |
| 56 |
|
| 57 |
impl Config { |
| 58 |
|
| 59 |
pub fn primary_bin(&self) -> &str { |
| 60 |
self.bin_names.first().map(|s| s.as_str()).unwrap_or("server") |
| 61 |
} |
| 62 |
|
| 63 |
pub fn load() -> Result<Self> { |
| 64 |
let path = std::env::var("SANDO_CONFIG").unwrap_or_else(|_| "sando-daemon.toml".into()); |
| 65 |
let raw = std::fs::read_to_string(&path) |
| 66 |
.with_context(|| format!("reading daemon config at {path}"))?; |
| 67 |
Ok(toml::from_str(&raw)?) |
| 68 |
} |
| 69 |
|
| 70 |
#[cfg(test)] |
| 71 |
pub fn for_tests() -> Self { |
| 72 |
Self { |
| 73 |
listen: "127.0.0.1:0".into(), |
| 74 |
db_path: PathBuf::from(":memory:"), |
| 75 |
topology_path: PathBuf::from("/tmp/sando-test-topology.toml"), |
| 76 |
workdir: PathBuf::from("/tmp/sando-test-workdir"), |
| 77 |
release_root: PathBuf::from("/tmp/sando-test-release-root"), |
| 78 |
scratch_db_url: None, |
| 79 |
bin_names: vec!["server".into()], |
| 80 |
logs_root: PathBuf::from("/tmp/sando-test-logs"), |
| 81 |
release_contents: Vec::new(), |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|