/
osx.go
216 lines (181 loc) · 4.87 KB
/
osx.go
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"text/template"
"github.com/Sirupsen/logrus"
"github.com/justone/pmb/api"
"github.com/kardianos/osext"
)
func handleOSXCommand(bus *pmb.PMB, command string, subcommand string, arguments []string) error {
var err error
logrus.Debugf("Handling %s with args of %s", command, arguments)
// launch agent name
agentName := fmt.Sprintf("org.endot.pmb.%s", subcommand)
logrus.Debugf("Name of launchagent: %s", agentName)
// figure out launch agent config path
launchAgentFile := fmt.Sprintf("%s/Library/LaunchAgents/%s.plist", os.Getenv("HOME"), agentName)
logrus.Debugf("launchagent file: %s", launchAgentFile)
var homeDir string
if homeDir = os.Getenv("HOME"); len(homeDir) == 0 {
homeDir = "/tmp"
}
// create launch data
executable, err := osext.Executable()
if err != nil {
return err
}
launchData := struct {
Name, Executable, Primary, HomeDir string
Args []string
}{
agentName, executable, bus.PrimaryURI(), homeDir, arguments,
}
switch command {
case "list":
fmt.Printf(`
Available commands for running '%s' as a background process (agent):
start - Starts agent via launchctl.
stop - Stops agent via launchctl.
restart - Restarts agent via launchctl.
configure - This will configure the agent, but not start it.
unconfigure - This will remove the agent configuration.
`, fmt.Sprintf("pmb %s", subcommand))
case "restart":
err = configure(launchAgentFile, generateLaunchConfig(launchData))
if err != nil {
return err
}
err = stop(launchAgentFile, agentName)
if err != nil {
return err
}
err = start(launchAgentFile, agentName)
if err != nil {
return err
}
case "stop":
err = configure(launchAgentFile, generateLaunchConfig(launchData))
if err != nil {
return err
}
err = stop(launchAgentFile, agentName)
if err != nil {
return err
}
err = unconfigure(launchAgentFile)
if err != nil {
return err
}
case "start":
err = configure(launchAgentFile, generateLaunchConfig(launchData))
if err != nil {
return err
}
err = start(launchAgentFile, agentName)
if err != nil {
return err
}
case "configure":
err = configure(launchAgentFile, generateLaunchConfig(launchData))
if err != nil {
return err
}
case "unconfigure":
err = unconfigure(launchAgentFile)
if err != nil {
return err
}
}
return nil
}
func start(launchAgentFile string, agentName string) error {
listCmd := exec.Command("/bin/launchctl", "list", agentName)
err := listCmd.Run()
if _, ok := err.(*exec.ExitError); ok {
// launch agent wasn't loaded yet, so load to start
startCmd := exec.Command("/bin/launchctl", "load", launchAgentFile)
startErr := startCmd.Run()
if startErr != nil {
return startErr
}
} else if err != nil {
// some error running the list command
return err
} else {
// launch agent was already loaded
logrus.Infof("Already running")
}
return nil
}
func stop(launchAgentFile string, agentName string) error {
listCmd := exec.Command("/bin/launchctl", "list", agentName)
err := listCmd.Run()
if err == nil {
// launch agent was loaded, so unload to stop
stopCmd := exec.Command("/bin/launchctl", "unload", launchAgentFile)
stopErr := stopCmd.Run()
if stopErr != nil {
return stopErr
}
} else if _, ok := err.(*exec.ExitError); ok {
// launch agent wasn't already loaded
logrus.Infof("Already stopped")
} else {
// some error running the list command
return err
}
return nil
}
func configure(launchAgentFile string, config string) error {
err := ioutil.WriteFile(launchAgentFile, []byte(config), 0644)
if err != nil {
return err
}
logrus.Debugf("Created %s: %s", launchAgentFile, config)
return nil
}
func generateLaunchConfig(launchData interface{}) string {
configureTemplate := `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>{{ .Name }}</string>
<key>KeepAlive</key>
<true/>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string>
<key>PMB_PRIMARY_URI</key>
<string>{{ .Primary }}</string>
</dict>
<key>StandardOutPath</key>
<string>{{ .HomeDir }}/.pmb.log</string>
<key>StandardErrorPath</key>
<string>{{ .HomeDir }}/.pmb.log</string>
<key>ProgramArguments</key>
<array>
<string>{{ .Executable }}</string>
{{ range .Args }}
<string>{{ . }}</string>
{{ end }}
</array>
</dict>
</plist>`
tmpl := template.Must(template.New("configure").Parse(configureTemplate))
var output bytes.Buffer
err := tmpl.Execute(&output, launchData)
if err != nil {
return ""
}
return output.String()
}
func unconfigure(launchAgentFile string) error {
logrus.Debugf("Removing %s", launchAgentFile)
return os.Remove(launchAgentFile)
}