From 7717d8ae0e891351b08e61b9e9a163d1f8b1cc11 Mon Sep 17 00:00:00 2001 From: id3v1669 Date: Sun, 7 Jul 2024 03:22:35 +0800 Subject: [PATCH] add changes from pr #249 commit: bbc69a8e8a15276415d4b45ce66675929f898aa2 --- swhkd/src/daemon.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/swhkd/src/daemon.rs b/swhkd/src/daemon.rs index 18c02e5..12788b0 100644 --- a/swhkd/src/daemon.rs +++ b/swhkd/src/daemon.rs @@ -95,6 +95,14 @@ async fn main() -> Result<(), Box> { log::debug!("Using config file path: {:#?}", config_file_path); + // If no config is present + // Creates a default config at location (read man 5 swhkd) + + if !Path::new(&config_file_path).exists() { + log::warn!("No config found at path: {:#?}", config_file_path); + create_default_config(invoking_uid, &config_file_path); + } + match config::load(&config_file_path) { Err(e) => { log::error!("Config Error: {}", e); @@ -494,6 +502,23 @@ pub fn setup_swhkd(invoking_uid: u32, runtime_path: String) { } } +pub fn create_default_config(invoking_uid: u32, config_file_path: &PathBuf) { + // Initializes a default SWHKD config at specific config path + + perms::raise_privileges(); + _ = match fs::File::create(&config_file_path) { + Ok(mut file) => { + log::debug!("Created default SWHKD config at: {:#?}", config_file_path); + _ = file.write_all(b"# Comments start with #, uncomment to use \n#start a terminal\n#super + return\n#\talacritty # replace with terminal of your choice"); + } + Err(err) => { + log::error!("Error creating config file: {}", err); + exit(1); + } + }; + perms::drop_privileges(invoking_uid); +} + pub fn send_command( hotkey: Hotkey, socket_path: &Path,