Move logging config to main configuration file.
- Move the content of what was stored in log4rs.yml into the main config file, so that there is just one file to parse to get the complete configuration. - Create the global switch `--home` to pass/overload the value for SEQUOIA_HOME in the configuration file.
This commit is contained in:
+26
-12
@@ -3,27 +3,36 @@
|
||||
//
|
||||
|
||||
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;
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct HuskConfig {
|
||||
pub connection: String,
|
||||
pub sequoia_home: String,
|
||||
logfile_config: Option<String>,
|
||||
pub keyservers: Option<Vec<String>>,
|
||||
logging: Option<toml::Value>,
|
||||
}
|
||||
|
||||
impl HuskConfig {
|
||||
|
||||
pub fn load(path: &str) -> anyhow::Result<Self> {
|
||||
pub fn load(path: &str, home: Option<String>) -> anyhow::Result<Self> {
|
||||
match fs::read_to_string(path) {
|
||||
Ok(data) => {
|
||||
match toml::from_str(data.as_str()) {
|
||||
Ok(configuration) => Ok(configuration),
|
||||
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())
|
||||
}
|
||||
@@ -35,14 +44,19 @@ impl HuskConfig {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn logfile_config(&self) -> String {
|
||||
match &self.logfile_config {
|
||||
Some(c) => c.clone(),
|
||||
None => {
|
||||
eprintln!("Using logging configuration from 'log4rs.yml'");
|
||||
"log4rs.yml".to_string()
|
||||
},
|
||||
}
|
||||
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> {
|
||||
|
||||
Reference in New Issue
Block a user