- Introduce option `--background` for `daemon start` to send the new process into the background. - Implement a `ProcessDescr` which identifies a Husk process. This pays attention to the possibility of a rollover of the process ids. - Implement `daemon status`. - Implement `daemon stop` which sends `SIG_INT` to a running Husk process.
121 lines
3.2 KiB
Rust
121 lines
3.2 KiB
Rust
//! Parsing the config file
|
|
|
|
use anyhow;
|
|
|
|
use std::fs;
|
|
use std::sync::{Arc, Mutex};
|
|
use serde_derive::Deserialize;
|
|
use toml;
|
|
use log4rs;
|
|
|
|
use crate::types::errors::HuskError;
|
|
use crate::types::defaults::CONSOLE_LOGGING;
|
|
use crate::types::defaults::PID_FILE_LOCATION;
|
|
use crate::types::defaults::SUBJECT_REPLACEMENT;
|
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
pub struct HuskConfig {
|
|
pub connection: String,
|
|
pub sequoia_home: String,
|
|
pub keyservers: Option<Vec<String>>,
|
|
pub subject_replacement: Option<String>,
|
|
pub pid_file: Option<String>,
|
|
logging: Option<toml::Value>,
|
|
}
|
|
|
|
impl HuskConfig {
|
|
|
|
pub fn load(path: &str, home: Option<String>) -> anyhow::Result<Self> {
|
|
match fs::read_to_string(path) {
|
|
Ok(data) => {
|
|
match toml::from_str::<HuskConfig>(data.as_str()) {
|
|
Ok(mut configuration) => {
|
|
if let Some(home) = home {
|
|
configuration.sequoia_home = home;
|
|
}
|
|
Ok(configuration)
|
|
},
|
|
Err(e) => {
|
|
Err(HuskError::ConfigFileParseError(e.to_string()).into())
|
|
}
|
|
}
|
|
},
|
|
Err(e) => {
|
|
Err(HuskError::ConfigFileOpenError(format!(
|
|
"{} - {}",
|
|
path,
|
|
e)).into())
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn init_logging(&self) -> anyhow::Result<()> {
|
|
let config = match toml::to_string(&self.logging) {
|
|
Ok(c) => c,
|
|
Err(e) => {
|
|
eprintln!("use default logging, because {}", e);
|
|
eprintln!("{:?}", self.logging);
|
|
CONSOLE_LOGGING.to_string()
|
|
}
|
|
};
|
|
let raw_config = toml::from_str::<log4rs::config::RawConfig>(config.as_str())?;
|
|
log4rs::init_raw_config(raw_config)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn get_keyservers(&self) -> Vec<String> {
|
|
if let Some(kss) = &self.keyservers {
|
|
kss.clone()
|
|
} else {
|
|
Vec::new()
|
|
}
|
|
}
|
|
|
|
pub fn get_subject_replacement(&self) -> String {
|
|
if let Some(replacement) = &self.subject_replacement {
|
|
replacement.clone()
|
|
} else {
|
|
SUBJECT_REPLACEMENT.to_string()
|
|
}
|
|
}
|
|
pub fn get_pid_file(&self) -> String {
|
|
if let Some(pid_file) = &self.pid_file {
|
|
pid_file.clone()
|
|
} else {
|
|
PID_FILE_LOCATION.to_string()
|
|
}
|
|
}
|
|
}
|
|
|
|
pub type HuskConfigContainer = Arc<Mutex<HuskConfig>>;
|
|
|
|
impl From<HuskConfigContainer> for HuskConfig {
|
|
fn from(container: HuskConfigContainer) -> Self {
|
|
let config = container.lock().unwrap();
|
|
config.clone()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::types::defaults;
|
|
|
|
use super::HuskConfig;
|
|
|
|
|
|
#[test]
|
|
fn subject_line() {
|
|
let config = HuskConfig {
|
|
connection: "".into(),
|
|
sequoia_home: "".into(),
|
|
keyservers: None,
|
|
subject_replacement: None,
|
|
logging: None,
|
|
pid_file: None,
|
|
};
|
|
|
|
assert_eq!(config.get_subject_replacement(), defaults::SUBJECT_REPLACEMENT);
|
|
}
|
|
}
|