-
Notifications
You must be signed in to change notification settings - Fork 30
/
schedule_darwin.go
91 lines (79 loc) · 2.38 KB
/
schedule_darwin.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
//+build darwin
package schedule
import (
"os"
"path"
"path/filepath"
"strings"
"github.com/creativeprojects/resticprofile/config"
"howett.net/plist"
)
const (
UserAgentPath = "Library/LaunchAgents"
GlobalAgentPath = "/Library/LaunchAgents"
GlobalDaemons = "/Library/LaunchDaemons"
)
type LaunchJob struct {
Label string `plist:"Label"`
Program string `plist:"Program"`
ProgramArguments []string `plist:"ProgramArguments"`
EnvironmentVariables map[string]string `plist:"EnvironmentVariables,omitempty"`
StandardInPath string `plist:"StandardInPath,omitempty"`
StandardOutPath string `plist:"StandardOutPath,omitempty"`
StandardErrorPath string `plist:"StandardErrorPath,omitempty"`
WorkingDirectory string `plist:"WorkingDirectory"`
StartInterval int `plist:"StartInterval,omitempty"`
StartCalendarInterval []CalendarInterval `plist:"StartCalendarInterval,omitempty"`
}
type CalendarInterval struct {
Month int `plist:"Month,omitempty"` // Month of year (1..12, 1 being January)
Day int `plist:"Day,omitempty"` // Day of month (1..31)
Weekday int `plist:"Weekday,omitempty"` // Day of week (0..7, 0 and 7 being Sunday)
Hour int `plist:"Hour,omitempty"` // Hour of day (0..23)
Minute int `plist:"Minute,omitempty"` // Minute of hour (0..59)
}
func CreateJob(config string, profile *config.Profile) error {
wd, err := os.Getwd()
if err != nil {
return err
}
home, err := os.UserHomeDir()
if err != nil {
return err
}
binary := os.Args[0]
if !filepath.IsAbs(binary) {
binary = path.Join(wd, binary)
}
name := "local.resticprofile." + strings.ToLower(profile.Name)
job := &LaunchJob{
Label: name,
Program: binary,
ProgramArguments: []string{
binary,
"--no-ansi",
"--config",
config,
"--name",
profile.Name,
"backup",
},
EnvironmentVariables: profile.Environment,
StandardOutPath: name + ".log",
StandardErrorPath: name + ".error.log",
WorkingDirectory: wd,
StartInterval: 300,
}
file, err := os.Create(path.Join(home, UserAgentPath, name+".agent.plist"))
if err != nil {
return err
}
defer file.Close()
encoder := plist.NewEncoder(file)
encoder.Indent("\t")
err = encoder.Encode(job)
if err != nil {
return err
}
return nil
}