This repository was archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplist.go
More file actions
96 lines (84 loc) · 2.33 KB
/
Copy pathplist.go
File metadata and controls
96 lines (84 loc) · 2.33 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
// +build darwin
package systemservice
import (
"bytes"
"path/filepath"
"text/template"
)
/*
plist represents a launchctl plist file
*/
type plist struct {
Label string
Program string
ProgramArguments []string
KeepAlive bool
RunAtLoad bool
StdOutPath string
StdErrPath string
}
func newPlist(serv *SystemService) plist {
label := serv.Command.Label
name := serv.Command.Name
logDir := filepath.Join(homeDir(), "Library/Logs", name)
if isRoot() {
logDir = filepath.Join("/Library/Logs", name)
}
args := []string{serv.Command.Program}
if len(serv.Command.Args) != 0 {
args = append(args, serv.Command.Args...)
}
pl := plist{
Label: label,
ProgramArguments: args,
KeepAlive: true,
RunAtLoad: true,
StdOutPath: filepath.Join(logDir, name+".stdout.log"),
StdErrPath: filepath.Join(logDir, name+".stderr.log"),
}
return pl
}
// TODO: Convert to io.Writer?
func (p *plist) Generate() (string, error) {
var tmpl bytes.Buffer
t := template.Must(template.New("launchdConfig").Parse(plistTemplate()))
if err := t.Execute(&tmpl, p); err != nil {
return "", err
}
return tmpl.String(), nil
}
func (p *plist) Path() string {
label := p.Label + ".plist"
if isRoot() {
return filepath.Join("/Library/LaunchDaemons/", label)
}
return filepath.Join(homeDir(), "Library/LaunchAgents/", label)
}
// func (p *plist) String() string {
// encoded, _ := xml.MarshalIndent(p, "", " ")
// return string(encoded)
// }
/*
plistTemplate generates the contents of the plist file.
*/
func plistTemplate() string {
return `<?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>{{ .Label }}</string>{{ if .Program }}
<key>Program</key><string>{{ .Program }}</string>{{ end }}
{{ if .ProgramArguments }}<key>ProgramArguments</key>
<array>{{ range $arg := .ProgramArguments }}
<string>{{ $arg }}</string>{{ end }}
</array>{{ end }}
<key>StandardOutPath</key>
<string>{{ .StdOutPath }}</string>
<key>StandardErrorPath</key>
<string>{{ .StdErrPath }}</string>
<key>KeepAlive</key> <{{ .KeepAlive }}/>
<key>RunAtLoad</key> <{{ .RunAtLoad }}/>
</dict>
</plist>
`
}