-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper_darwin_test.go
60 lines (53 loc) · 1.27 KB
/
wrapper_darwin_test.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
package service
import (
"bufio"
"os"
"os/exec"
"strings"
"time"
)
const goSpfcTestPlist string = "<?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>go-spfc-test</string> " +
" <key>ProgramArguments</key> " +
" <array> " +
" <string>bash</string> " +
" <string>-c</string> " +
" <string>sleep 60</string> " +
" </array> " +
" <key>UserName</key> " +
" <string>'$USER'</string> " +
"</dict> " +
"</plist> "
func check(e error) {
if e != nil {
panic(e)
}
}
func createService() {
s := []string{os.Getenv("HOME"), "/Library/LaunchAgents/", servNameTest, ".plist"}
servNameTestPList := strings.Join(s, "")
f, err := os.Create(servNameTestPList)
w := bufio.NewWriter(f)
_, err = w.WriteString(goSpfcTestPlist)
w.Flush()
check(err)
execLaunchctl("load", servNameTestPList)
}
func removeService() {
execLaunchctl("remove", servNameTest)
}
func waitStop() {
// Wait a little bit
time.Sleep(2 * time.Second)
}
func execLaunchctl(arg ...string) {
cmd := exec.Command("launchctl", arg...)
out, err := cmd.CombinedOutput()
if err != nil {
panic(string(out))
}
}