Repository navigation
Expand file tree
/
Copy pathservices_darwin.go
More file actions
226 lines (184 loc) · 4.81 KB
/
Copy pathservices_darwin.go
File metadata and controls
226 lines (184 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//go:build darwin
package cli
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
)
const (
serviceLabel = "com.y3owk1n.neru"
launchAgentsDir = "~/Library/LaunchAgents"
plistFile = launchAgentsDir + "/" + serviceLabel + ".plist"
)
const plistTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.y3owk1n.neru</string>
<key>ProgramArguments</key>
<array>
<string>NERU_BINARY_PATH</string>
<string>launch</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/neru.log</string>
<key>StandardErrorPath</key>
<string>/tmp/neru.err.log</string>
<key>ProcessType</key>
<string>Interactive</string>
<key>LimitLoadToSessionType</key>
<string>Aqua</string>
<key>Nice</key>
<integer>-10</integer>
<key>ThrottleInterval</key>
<integer>10</integer>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin</string>
</dict>
</dict>
</plist>`
var (
errServiceAlreadyLoaded = errors.New(
"service is already loaded; check for existing installations (e.g., nix-darwin, home-manager) and uninstall them first",
)
errPlistAlreadyExists = errors.New("plist file already exists")
)
func getBinaryPath() (string, error) {
execPath, err := os.Executable()
if err != nil {
return "", err
}
return filepath.EvalSymlinks(execPath)
}
func isServiceLoaded() bool {
cmd := exec.CommandContext(context.Background(), "launchctl", "list", serviceLabel)
return cmd.Run() == nil
}
func installService() error {
// Check if service is already loaded
if isServiceLoaded() {
return errServiceAlreadyLoaded
}
binPath, err := getBinaryPath()
if err != nil {
return fmt.Errorf("failed to get binary path: %w", err)
}
plistContent := strings.ReplaceAll(plistTemplate, "NERU_BINARY_PATH", binPath)
expandedDir, err := expandPath(launchAgentsDir)
if err != nil {
return fmt.Errorf("failed to expand LaunchAgents path: %w", err)
}
// Check if plist already exists
expandedPlist := filepath.Join(expandedDir, serviceLabel+".plist")
_, statErr := os.Stat(expandedPlist)
if statErr == nil {
return fmt.Errorf(
"%w at %s; remove it manually or uninstall first",
errPlistAlreadyExists,
expandedPlist,
)
}
// Ensure directory exists
const dirPerm = 0o755
err = os.MkdirAll(expandedDir, dirPerm)
if err != nil {
return fmt.Errorf("failed to create LaunchAgents directory: %w", err)
}
// Write plist
const filePerm = 0o644
err = os.WriteFile(expandedPlist, []byte(plistContent), filePerm)
if err != nil {
return fmt.Errorf("failed to write plist: %w", err)
}
currentUser, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get current user: %w", err)
}
// Load service
cmd := exec.CommandContext(
context.Background(),
"launchctl",
"bootstrap",
"gui/"+currentUser.Uid,
expandedPlist,
)
err = cmd.Run()
if err != nil {
return fmt.Errorf("failed to load service: %w", err)
}
return nil
}
func uninstallService() error {
expandedPlist, err := expandPath(plistFile)
if err != nil {
return fmt.Errorf("failed to expand plist path: %w", err)
}
currentUser, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get current user: %w", err)
}
// Unload service if loaded
cmd := exec.CommandContext(
context.Background(),
"launchctl",
"bootout",
"gui/"+currentUser.Uid+"/"+serviceLabel,
)
_ = cmd.Run() // Ignore error if not loaded
err = os.Remove(expandedPlist)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to remove plist: %w", err)
}
return nil
}
func startService() error {
cmd := exec.CommandContext(context.Background(), "launchctl", "start", serviceLabel)
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to start service: %w", err)
}
return nil
}
func stopService() error {
cmd := exec.CommandContext(context.Background(), "launchctl", "stop", serviceLabel)
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to stop service: %w", err)
}
return nil
}
func restartService() error {
_ = stopService()
// Always attempt to start
return startService()
}
func statusService() string {
cmd := exec.CommandContext(context.Background(), "launchctl", "list", serviceLabel)
_, err := cmd.Output()
if err != nil {
return "Service not loaded"
}
return "Service loaded"
}
func expandPath(path string) (string, error) {
if strings.HasPrefix(path, "~") {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, path[1:]), nil
}
return path, nil
}