From 2a038749059da3f5659c6735230e32324e5011ec Mon Sep 17 00:00:00 2001 From: Fred Date: Wed, 1 Jul 2020 19:35:24 +0100 Subject: [PATCH] create plist file for darwin --- commands.go | 25 ++++++++++++++ go.mod | 2 +- schedule/schedule_darwin.go | 63 +++++++++++++++++++++++++++++++++++- schedule/schedule_systemd.go | 9 ++++++ schedule/schedule_windows.go | 9 ++++++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 schedule/schedule_systemd.go create mode 100644 schedule/schedule_windows.go diff --git a/commands.go b/commands.go index abf5f562..bdff0552 100644 --- a/commands.go +++ b/commands.go @@ -7,8 +7,10 @@ import ( "strings" "text/tabwriter" + "github.com/creativeprojects/resticprofile/clog" "github.com/creativeprojects/resticprofile/config" "github.com/creativeprojects/resticprofile/constants" + "github.com/creativeprojects/resticprofile/schedule" "github.com/creativeprojects/resticprofile/systemd" ) @@ -54,6 +56,13 @@ var ( needConfiguration: false, hide: true, }, + { + name: "schedule", + description: "schedule a backup", + action: createSchedule, + needConfiguration: true, + hide: false, + }, } ) @@ -178,3 +187,19 @@ func showProfile(c *config.Config, flags commandLineFlags, args []string) error config.ShowStruct(os.Stdout, profile) return nil } + +func createSchedule(c *config.Config, flags commandLineFlags, args []string) error { + profile, err := c.GetProfile(flags.name) + if err != nil { + return fmt.Errorf("cannot load profile '%s': %w", flags.name, err) + } + if profile == nil { + return fmt.Errorf("profile '%s' not found", flags.name) + } + + err = schedule.CreateJob(flags.config, profile) + if err == nil { + clog.Info("job created!") + } + return err +} diff --git a/go.mod b/go.mod index ed8159ab..5b931408 100644 --- a/go.mod +++ b/go.mod @@ -35,5 +35,5 @@ require ( gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect gopkg.in/ini.v1 v1.55.0 // indirect gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c // indirect - howett.net/plist v0.0.0-20200419221736-3b63eb3a43b5 // indirect + howett.net/plist v0.0.0-20200419221736-3b63eb3a43b5 ) diff --git a/schedule/schedule_darwin.go b/schedule/schedule_darwin.go index bbd1af42..97c4c427 100644 --- a/schedule/schedule_darwin.go +++ b/schedule/schedule_darwin.go @@ -2,8 +2,18 @@ package schedule +import ( + "os" + "path" + "path/filepath" + "strings" + + "github.com/creativeprojects/resticprofile/config" + "howett.net/plist" +) + const ( - UserAgentPath = "~/Library/LaunchAgents" + UserAgentPath = "Library/LaunchAgents" GlobalAgentPath = "/Library/LaunchAgents" GlobalDaemons = "/Library/LaunchDaemons" ) @@ -28,3 +38,54 @@ type CalendarInterval struct { 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 +} diff --git a/schedule/schedule_systemd.go b/schedule/schedule_systemd.go new file mode 100644 index 00000000..902f7add --- /dev/null +++ b/schedule/schedule_systemd.go @@ -0,0 +1,9 @@ +//+build !darwin,!windows + +package schedule + +import "github.com/creativeprojects/resticprofile/config" + +func CreateJob(config string, profile *config.Profile) error { + return nil +} diff --git a/schedule/schedule_windows.go b/schedule/schedule_windows.go new file mode 100644 index 00000000..d3ab8158 --- /dev/null +++ b/schedule/schedule_windows.go @@ -0,0 +1,9 @@ +//+build windows + +package schedule + +import "github.com/creativeprojects/resticprofile/config" + +func CreateJob(config string, profile *config.Profile) error { + return nil +}