Skip to content

Commit

Permalink
feat: generate default config if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Parag003 committed Feb 29, 2024
1 parent 994b74f commit bbc69a8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ async fn main() -> Result<(), Box<dyn Error>> {

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);
Expand Down Expand Up @@ -501,6 +509,23 @@ pub fn set_command_line_args() -> Command<'static> {
app
}

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 fetch_xdg_config_path() -> PathBuf {
let config_file_path: PathBuf = match env::var("XDG_CONFIG_HOME") {
Ok(val) => {
Expand Down

0 comments on commit bbc69a8

Please sign in to comment.