-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplist.go
43 lines (38 loc) · 1.05 KB
/
plist.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
package launchd
import (
"bytes"
"fmt"
"os"
// Imported for embedded launchagent template below.
_ "embed"
"text/template"
)
//go:embed launchagent.tmpl
var plistTemplateSrc string
var plistTemplate = template.Must(
template.New("launchctl").Parse(plistTemplateSrc),
)
// RenderPlist returns the contents of a plist file content for a launchd service.
func (s *Service) RenderPlist() ([]byte, error) {
var buf bytes.Buffer
err := plistTemplate.Execute(&buf, *s)
return buf.Bytes(), err
}
// WritePlist writes the contents of the service's plist file content to the appropriate directory.
func (s *Service) WritePlist(content []byte) error {
content, err := s.RenderPlist()
if err != nil {
return fmt.Errorf("could not render Plist: %w", err)
}
path, err := s.DefinitionPath()
if err != nil {
return fmt.Errorf("could not find definition path: %w", err)
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("could not create %s: %w", path, err)
}
defer f.Close()
_, err = f.Write(content)
return err
}