Skip to content

Commit

Permalink
create plist file for darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Jul 1, 2020
1 parent 4315bc6 commit 2a03874
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
25 changes: 25 additions & 0 deletions commands.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -54,6 +56,13 @@ var (
needConfiguration: false,
hide: true,
},
{
name: "schedule",
description: "schedule a backup",
action: createSchedule,
needConfiguration: true,
hide: false,
},
}
)

Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -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
)
63 changes: 62 additions & 1 deletion schedule/schedule_darwin.go
Expand Up @@ -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"
)
Expand All @@ -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
}
9 changes: 9 additions & 0 deletions 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
}
9 changes: 9 additions & 0 deletions 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
}

0 comments on commit 2a03874

Please sign in to comment.