Skip to main content

pitchfork_cli/
boot_manager.rs

1use crate::{Result, env};
2use auto_launcher::{AutoLaunch, AutoLaunchBuilder, LinuxLaunchMode, MacOSLaunchMode};
3use miette::IntoDiagnostic;
4
5pub struct BootManager {
6    auto_launcher: AutoLaunch,
7}
8
9impl BootManager {
10    pub fn new() -> Result<Self> {
11        let app_name = "pitchfork";
12        let app_path = env::PITCHFORK_BIN.to_string_lossy().to_string();
13
14        let auto_launcher = AutoLaunchBuilder::new()
15            .set_app_name(app_name)
16            .set_app_path(&app_path)
17            .set_macos_launch_mode(MacOSLaunchMode::LaunchAgent)
18            .set_linux_launch_mode(LinuxLaunchMode::Systemd)
19            .set_args(&["supervisor", "run", "--boot"])
20            .build()
21            .into_diagnostic()?;
22
23        Ok(Self { auto_launcher })
24    }
25
26    pub fn is_enabled(&self) -> Result<bool> {
27        self.auto_launcher.is_enabled().into_diagnostic()
28    }
29
30    pub fn enable(&self) -> Result<()> {
31        self.auto_launcher.enable().into_diagnostic()?;
32        Ok(())
33    }
34
35    pub fn disable(&self) -> Result<()> {
36        self.auto_launcher.disable().into_diagnostic()?;
37        Ok(())
38    }
39}