File: autostart_darwin.go

package info (click to toggle)
golang-github-protonmail-go-autostart 0.0~git20181114.c527205-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 80 kB
  • sloc: ansic: 38; makefile: 2
file content (66 lines) | stat: -rw-r--r-- 1,274 bytes parent folder | download
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
package autostart

import (
	"os"
	"path/filepath"
	"text/template"
)

const jobTemplate = `<?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>{{.Name}}</string>
    <key>ProgramArguments</key>
      <array>
        {{range .Exec -}}
        <string>{{.}}</string>
        {{end}}
      </array>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>`

var launchDir string

func init() {
	launchDir = filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents")
}

func (a *App) path() string {
	return filepath.Join(launchDir, a.Name+".plist")
}

// IsEnabled Check is app enabled startup.
func (a *App) IsEnabled() bool {
	_, err := os.Stat(a.path())
	return err == nil
}

// Enable this app on startup.
func (a *App) Enable() error {
	t := template.Must(template.New("job").Parse(jobTemplate))

	if err := os.MkdirAll(launchDir, 0777); err != nil {
		return err
	}
	f, err := os.Create(a.path())
	if err != nil {
		return err
	}
	defer f.Close()

	if err := t.Execute(f, a); err != nil {
		return err
	}

	return nil
}

// Disable this app on startup.
func (a *App) Disable() error {

	return os.Remove(a.path())
}