pub fn install() -> anyhow::Result<()> {
let exe = moadim_exe()?;
let log = daemon_log();
let home = crate::paths::home();
let plist = plist_path_from_home(home.clone())?;
#[allow(
clippy::expect_used,
reason = "the `?` above already returned if `home` were `None` (see \
`plist_path_from_home`), so this is a proven invariant, not a real failure path"
)]
let home = home.expect("plist_path_from_home errors before this point when home is None");
let working_dir = std::env::current_dir()?;
write_plist(&plist, &exe, &log, &home, &working_dir)?;
reload_agent(&plist)?;
report_installed(&plist, &log);
request_automation_permission();
Ok(())
}
fn request_automation_permission() {
println!(
" hint if macOS asks \"moadim would like to administer your computer\", click OK — \
granting it here prevents background interruptions"
);
let _ = std::process::Command::new("osascript")
.args([
"-e",
"tell application \"System Events\" to get name of every process",
])
.output();
}
pub fn uninstall() -> anyhow::Result<()> {
let plist = plist_path()?;
if plist.exists() {
let plist_arg = plist.display().to_string();
let _ = run(&launchctl_bin(), &["unload", "-w", &plist_arg]);
std::fs::remove_file(&plist)?;
println!("moadim launchd agent removed ({})", plist.display());
} else {
println!(
"moadim launchd agent is not installed (no plist at {})",
plist.display()
);
}
Ok(())
}