|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +package autostart |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/xml" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + launchAgentLabel = "com.debrief" |
| 15 | + plistName = launchAgentLabel + ".plist" |
| 16 | + plistDir = "Library/LaunchAgents" |
| 17 | + launchAgentsDirPerm = 0o750 |
| 18 | + plistFilePerm = 0o600 |
| 19 | +) |
| 20 | + |
| 21 | +// Enable registers the app as a macOS LaunchAgent to start on login. |
| 22 | +func Enable() error { |
| 23 | + exePath, err := os.Executable() |
| 24 | + if err != nil { |
| 25 | + return fmt.Errorf("failed to get executable path: %w", err) |
| 26 | + } |
| 27 | + |
| 28 | + plistPath, err := launchAgentPath() |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + |
| 33 | + dir := filepath.Dir(plistPath) |
| 34 | + if err := os.MkdirAll(dir, launchAgentsDirPerm); err != nil { |
| 35 | + return fmt.Errorf("failed to create LaunchAgents directory: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + var escaped strings.Builder |
| 39 | + if err := xml.EscapeText(&escaped, []byte(exePath)); err != nil { |
| 40 | + return fmt.Errorf("failed to escape executable path: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + content := `<?xml version="1.0" encoding="UTF-8"?> |
| 44 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 45 | +<plist version="1.0"> |
| 46 | +<dict> |
| 47 | + <key>Label</key> |
| 48 | + <string>` + launchAgentLabel + `</string> |
| 49 | + <key>ProgramArguments</key> |
| 50 | + <array> |
| 51 | + <string>` + escaped.String() + `</string> |
| 52 | + </array> |
| 53 | + <key>RunAtLoad</key> |
| 54 | + <true/> |
| 55 | +</dict> |
| 56 | +</plist> |
| 57 | +` |
| 58 | + |
| 59 | + if err := os.WriteFile(plistPath, []byte(content), plistFilePerm); err != nil { |
| 60 | + return fmt.Errorf("failed to write LaunchAgent plist: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +// Disable removes the LaunchAgent plist to stop the app from starting on login. |
| 67 | +func Disable() error { |
| 68 | + plistPath, err := launchAgentPath() |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + if err := os.Remove(plistPath); err != nil { |
| 74 | + if os.IsNotExist(err) { |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + return fmt.Errorf("failed to remove LaunchAgent plist: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +// IsEnabled checks whether the LaunchAgent plist exists. |
| 85 | +func IsEnabled() (bool, error) { |
| 86 | + plistPath, err := launchAgentPath() |
| 87 | + if err != nil { |
| 88 | + return false, err |
| 89 | + } |
| 90 | + |
| 91 | + _, err = os.Stat(plistPath) |
| 92 | + if err != nil { |
| 93 | + if os.IsNotExist(err) { |
| 94 | + return false, nil |
| 95 | + } |
| 96 | + |
| 97 | + return false, fmt.Errorf("failed to check LaunchAgent plist: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + return true, nil |
| 101 | +} |
| 102 | + |
| 103 | +func launchAgentPath() (string, error) { |
| 104 | + home, err := os.UserHomeDir() |
| 105 | + if err != nil { |
| 106 | + return "", fmt.Errorf("failed to get home directory: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + return filepath.Join(home, plistDir, plistName), nil |
| 110 | +} |
0 commit comments