From b83654923256d560cd1ffb163cb807805526feef Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Tue, 27 Jul 2021 17:38:01 +0200 Subject: [PATCH] Don't exit daemon when initial load fails --- crates/eww/src/config/eww_config.rs | 9 ++++++++ crates/eww/src/server.rs | 32 +++++++++++++++++++---------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/crates/eww/src/config/eww_config.rs b/crates/eww/src/config/eww_config.rs index b44f102..ffc275b 100644 --- a/crates/eww/src/config/eww_config.rs +++ b/crates/eww/src/config/eww_config.rs @@ -18,8 +18,17 @@ pub struct EwwConfig { script_vars: HashMap, } +impl Default for EwwConfig { + fn default() -> Self { + Self { widgets: HashMap::new(), windows: HashMap::new(), initial_variables: HashMap::new(), script_vars: HashMap::new() } + } +} + impl EwwConfig { pub fn read_from_file(files: &mut YuckFiles, path: impl AsRef) -> Result { + if !path.as_ref().exists() { + bail!("The configuration file `{}` does not exist", path.as_ref().display()); + } let config = Config::generate_from_main_file(files, path)?; let Config { widget_definitions, window_definitions, var_definitions, mut script_vars } = config; script_vars.extend(crate::config::inbuilt::get_inbuilt_vars()); diff --git a/crates/eww/src/server.rs b/crates/eww/src/server.rs index 20f9b77..1457382 100644 --- a/crates/eww/src/server.rs +++ b/crates/eww/src/server.rs @@ -10,6 +10,27 @@ use std::{ use tokio::sync::mpsc::*; pub fn initialize_server(paths: EwwPaths) -> Result<()> { + let (ui_send, mut ui_recv) = tokio::sync::mpsc::unbounded_channel(); + + std::env::set_current_dir(&paths.get_config_dir()) + .with_context(|| format!("Failed to change working directory to {}", paths.get_config_dir().display()))?; + + log::info!("Loading paths: {}", &paths); + + // disgusting global state, I hate this, but https://github.com/buffet told me that this is what I should do for peak maintainability + error_handling_ctx::clear_files(); + + let read_config = + config::EwwConfig::read_from_file(&mut error_handling_ctx::ERROR_HANDLING_CTX.lock().unwrap(), &paths.get_yuck_path()); + + let eww_config = match read_config { + Ok(config) => config, + Err(err) => { + error_handling_ctx::print_error(err); + config::EwwConfig::default() + } + }; + do_detach(&paths.get_log_file())?; println!( @@ -27,17 +48,6 @@ pub fn initialize_server(paths: EwwPaths) -> Result<()> { std::process::exit(1); } }); - let (ui_send, mut ui_recv) = tokio::sync::mpsc::unbounded_channel(); - - std::env::set_current_dir(&paths.get_config_dir()) - .with_context(|| format!("Failed to change working directory to {}", paths.get_config_dir().display()))?; - - log::info!("Loading paths: {}", &paths); - - // disgusting global state, I hate this, but https://github.com/buffet told me that this is what I should do for peak maintainability - error_handling_ctx::clear_files(); - let eww_config = - config::EwwConfig::read_from_file(&mut error_handling_ctx::ERROR_HANDLING_CTX.lock().unwrap(), &paths.get_yuck_path())?; gtk::init()?;