-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] environment for swhkd (#246)
* refactor: New Env for swhkd * Refactor Functions * Fix Spelling in environ * Fix spelling in daemon * Add Path Validations * Run formatting
- Loading branch information
1 parent
a4d2ffe
commit 5e8ebe3
Showing
3 changed files
with
158 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
use std::{ | ||
env::VarError, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
pub struct Env { | ||
pub pkexec_id: u32, | ||
pub xdg_config_home: PathBuf, | ||
pub xdg_runtime_socket: PathBuf, | ||
pub xdg_runtime_dir: PathBuf, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum EnvError { | ||
PkexecNotFound, | ||
XdgConfigNotFound, | ||
XdgRuntimeNotFound, | ||
PathNotFound, | ||
GenericError(String), | ||
} | ||
|
||
impl Env { | ||
pub fn construct() -> Self { | ||
let pkexec_id = match Self::get_env("PKEXEC_UID") { | ||
Ok(val) => match val.parse::<u32>() { | ||
Ok(val) => val, | ||
Err(_) => { | ||
log::error!("Failed to launch swhkd!!!"); | ||
log::error!("Make sure to launch the binary with pkexec."); | ||
std::process::exit(1); | ||
} | ||
}, | ||
Err(_) => { | ||
log::error!("Failed to launch swhkd!!!"); | ||
log::error!("Make sure to launch the binary with pkexec."); | ||
std::process::exit(1); | ||
} | ||
}; | ||
|
||
let xdg_config_home = match Self::get_env("XDG_CONFIG_HOME") { | ||
Ok(val) => match validate_path(&PathBuf::from(val)) { | ||
Ok(val) => val, | ||
Err(e) => match e { | ||
EnvError::PathNotFound => { | ||
log::warn!("XDG_CONFIG_HOME does not exist, using hardcoded /etc"); | ||
PathBuf::from("/etc") | ||
} | ||
_ => { | ||
eprintln!("Failed to get XDG_CONFIG_HOME: {:?}", e); | ||
std::process::exit(1); | ||
} | ||
}, | ||
}, | ||
Err(e) => match e { | ||
EnvError::XdgConfigNotFound => { | ||
log::warn!("XDG_CONFIG_HOME not found, using hardcoded /etc"); | ||
PathBuf::from("/etc") | ||
} | ||
_ => { | ||
eprintln!("Failed to get XDG_CONFIG_HOME: {:?}", e); | ||
std::process::exit(1); | ||
} | ||
}, | ||
}; | ||
|
||
let xdg_runtime_socket = match Self::get_env("XDG_RUNTIME_DIR") { | ||
Ok(val) => match validate_path(&PathBuf::from(val).join("swhkd.sock")) { | ||
Ok(val) => val, | ||
Err(e) => match e { | ||
EnvError::PathNotFound => { | ||
log::warn!("XDG_RUNTIME_DIR does not exist, using hardcoded /run/user"); | ||
PathBuf::from(format!("/run/user/{}", pkexec_id)) | ||
} | ||
_ => { | ||
eprintln!("Failed to get XDG_RUNTIME_DIR: {:?}", e); | ||
std::process::exit(1); | ||
} | ||
}, | ||
}, | ||
Err(e) => match e { | ||
EnvError::XdgRuntimeNotFound => { | ||
log::warn!("XDG_RUNTIME_DIR not found, using hardcoded /run/user"); | ||
PathBuf::from(format!("/run/user/{}", pkexec_id)) | ||
} | ||
_ => { | ||
eprintln!("Failed to get XDG_RUNTIME_DIR: {:?}", e); | ||
std::process::exit(1); | ||
} | ||
}, | ||
}; | ||
|
||
let xdg_runtime_dir = match Self::get_env("XDG_RUNTIME_DIR") { | ||
Ok(val) => PathBuf::from(val), | ||
Err(e) => match e { | ||
EnvError::XdgRuntimeNotFound => { | ||
log::warn!("XDG_RUNTIME_DIR not found, using hardcoded /run/swhkd"); | ||
PathBuf::from("/run/swhkd") | ||
} | ||
_ => { | ||
eprintln!("Failed to get XDG_RUNTIME_DIR: {:?}", e); | ||
std::process::exit(1); | ||
} | ||
}, | ||
}; | ||
|
||
Self { pkexec_id, xdg_config_home, xdg_runtime_dir, xdg_runtime_socket } | ||
} | ||
|
||
fn get_env(name: &str) -> Result<String, EnvError> { | ||
match std::env::var(name) { | ||
Ok(val) => Ok(val), | ||
Err(e) => match e { | ||
VarError::NotPresent => match name { | ||
"PKEXEC_UID" => Err(EnvError::PkexecNotFound), | ||
"XDG_CONFIG_HOME" => Err(EnvError::XdgConfigNotFound), | ||
"XDG_RUNTIME_DIR" => Err(EnvError::XdgRuntimeNotFound), | ||
_ => Err(EnvError::GenericError(e.to_string())), | ||
}, | ||
VarError::NotUnicode(_) => { | ||
Err(EnvError::GenericError("Not a valid unicode".to_string())) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
pub fn fetch_xdg_config_path(&self) -> PathBuf { | ||
PathBuf::from(&self.xdg_config_home).join("swhkd/swhkdrc") | ||
} | ||
|
||
pub fn fetch_xdg_runtime_socket_path(&self) -> PathBuf { | ||
PathBuf::from(&self.xdg_runtime_dir).join("swhkd.sock") | ||
} | ||
} | ||
|
||
fn validate_path(path: &Path) -> Result<PathBuf, EnvError> { | ||
if path.exists() { | ||
Ok(path.to_path_buf()) | ||
} else { | ||
Err(EnvError::PathNotFound) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters