diff --git a/src/main.rs b/src/main.rs index 687e961..e727b37 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,11 @@ fn main() { .init(); let result: Result<_> = try { - let paths = opts.config_path.map(EwwPaths::from_config_dir).unwrap_or_default(); + let paths = opts + .config_path + .map(EwwPaths::from_config_dir) + .unwrap_or_else(EwwPaths::default) + .context("Failed set paths")?; match opts.action { opts::Action::ClientOnly(action) => { @@ -115,11 +119,20 @@ pub struct EwwPaths { } impl EwwPaths { - pub fn from_config_dir>(config_dir: P) -> Self { - let daemon_id = base64::encode(format!("{}", config_dir.as_ref().display())); + pub fn from_config_dir>(config_dir: P) -> Result { + let config_dir = config_dir.as_ref(); + let config_dir = if config_dir.is_file() { + config_dir + .parent() + .context("Given config file did not have a parent directory")? + } else { + config_dir + }; + let config_dir = config_dir.canonicalize()?; + let daemon_id = base64::encode(format!("{}", config_dir.display())); - EwwPaths { - config_dir: config_dir.as_ref().to_path_buf(), + Ok(EwwPaths { + config_dir: config_dir.to_path_buf(), log_file: std::env::var("XDG_CACHE_HOME") .map(PathBuf::from) .unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".cache")) @@ -128,7 +141,16 @@ impl EwwPaths { .map(std::path::PathBuf::from) .unwrap_or_else(|_| std::path::PathBuf::from("/tmp")) .join(format!("eww-server_{}", daemon_id)), - } + }) + } + + pub fn default() -> Result { + let config_dir = std::env::var("XDG_CONFIG_HOME") + .map(PathBuf::from) + .unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".config")) + .join("eww"); + + Self::from_config_dir(config_dir) } pub fn get_log_file(&self) -> &Path { @@ -152,17 +174,6 @@ impl EwwPaths { } } -impl Default for EwwPaths { - fn default() -> Self { - let config_dir = std::env::var("XDG_CONFIG_HOME") - .map(PathBuf::from) - .unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".config")) - .join("eww"); - - Self::from_config_dir(config_dir) - } -} - impl std::fmt::Display for EwwPaths { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(