diff --git a/crond/crontab_test.go b/crond/crontab_test.go index d2575978..59b967c3 100644 --- a/crond/crontab_test.go +++ b/crond/crontab_test.go @@ -23,13 +23,24 @@ func TestGenerateSimpleCrontab(t *testing.T) { crontab := NewCrontab([]Entry{NewEntry(calendar.NewEvent(func(event *calendar.Event) { event.Minute.MustAddValue(1) event.Hour.MustAddValue(1) - }), "", "", "", "resticprofile backup")}) + }), "", "", "", "resticprofile backup", "")}) buffer := &strings.Builder{} err := crontab.Generate(buffer) require.NoError(t, err) assert.Equal(t, "01 01 * * *\tresticprofile backup\n", buffer.String()) } +func TestGenerateWorkDirCrontab(t *testing.T) { + crontab := NewCrontab([]Entry{NewEntry(calendar.NewEvent(func(event *calendar.Event) { + event.Minute.MustAddValue(1) + event.Hour.MustAddValue(1) + }), "", "", "", "resticprofile backup", "workdir")}) + buffer := &strings.Builder{} + err := crontab.Generate(buffer) + require.NoError(t, err) + assert.Equal(t, "01 01 * * *\tcd workdir && resticprofile backup\n", buffer.String()) +} + func TestCleanupCrontab(t *testing.T) { crontab := `# DO NOT EDIT THIS FILE - edit the master and reinstall. # (/tmp/crontab.pMvuGY/crontab installed on Wed Jan 13 12:08:43 2021) @@ -107,7 +118,7 @@ func TestUpdateSimpleCrontab(t *testing.T) { crontab := NewCrontab([]Entry{NewEntry(calendar.NewEvent(func(event *calendar.Event) { event.Minute.MustAddValue(1) event.Hour.MustAddValue(1) - }), "", "", "", "resticprofile backup")}) + }), "", "", "", "resticprofile backup", "")}) buffer := &strings.Builder{} err := crontab.Update("", true, buffer) require.NoError(t, err) @@ -118,7 +129,7 @@ func TestUpdateExistingCrontab(t *testing.T) { crontab := NewCrontab([]Entry{NewEntry(calendar.NewEvent(func(event *calendar.Event) { event.Minute.MustAddValue(1) event.Hour.MustAddValue(1) - }), "", "", "", "resticprofile backup")}) + }), "", "", "", "resticprofile backup", "")}) buffer := &strings.Builder{} err := crontab.Update("something\n"+startMarker+endMarker, true, buffer) require.NoError(t, err) diff --git a/crond/entry.go b/crond/entry.go index d0780bf1..a493546a 100644 --- a/crond/entry.go +++ b/crond/entry.go @@ -17,22 +17,28 @@ type Entry struct { profileName string commandName string commandLine string + workDir string } // NewEntry creates a new crontab entry -func NewEntry(event *calendar.Event, configFile, profileName, commandName, commandLine string) Entry { +func NewEntry(event *calendar.Event, configFile, profileName, commandName, commandLine, workDir string) Entry { return Entry{ event: event, configFile: configFile, profileName: profileName, commandName: commandName, commandLine: commandLine, + workDir: workDir, } } // String returns the crontab line representation of the entry (end of line included) func (e Entry) String() string { minute, hour, dayOfMonth, month, dayOfWeek := "*", "*", "*", "*", "*" + wd := "" + if e.workDir != "" { + wd = fmt.Sprintf("cd %s && ", e.workDir) + } if e.event.Minute.HasValue() { minute = formatRange(e.event.Minute.GetRanges(), twoDecimals) } @@ -49,7 +55,7 @@ func (e Entry) String() string { // don't make ranges for days of the week as it can fail with high sunday (7) dayOfWeek = formatList(e.event.WeekDay.GetRangeValues(), formatWeekDay) } - return fmt.Sprintf("%s %s %s %s %s\t%s\n", minute, hour, dayOfMonth, month, dayOfWeek, e.commandLine) + return fmt.Sprintf("%s %s %s %s %s\t%s%s\n", minute, hour, dayOfMonth, month, dayOfWeek, wd, e.commandLine) } // Generate writes a cron line in the StringWriter (end of line included) diff --git a/crond/entry_test.go b/crond/entry_test.go index 86957621..1007b3c0 100644 --- a/crond/entry_test.go +++ b/crond/entry_test.go @@ -12,7 +12,7 @@ import ( ) func TestEmptyUserEvent(t *testing.T) { - entry := NewEntry(calendar.NewEvent(), "", "", "", "command line") + entry := NewEntry(calendar.NewEvent(), "", "", "", "command line", "") buffer := &strings.Builder{} err := entry.Generate(buffer) require.NoError(t, err) @@ -62,7 +62,7 @@ func TestEvents(t *testing.T) { err := event.Parse(testRun.event) require.NoError(t, err) - entry := NewEntry(event, "", "", "", "command line") + entry := NewEntry(event, "", "", "", "command line", "") buffer := &strings.Builder{} err = entry.Generate(buffer) require.NoError(t, err) diff --git a/schedule/schedule_crond.go b/schedule/schedule_crond.go index c6f9fd18..fa2e17e7 100644 --- a/schedule/schedule_crond.go +++ b/schedule/schedule_crond.go @@ -17,7 +17,14 @@ const ( func (j *Job) createCrondJob(schedules []*calendar.Event) error { entries := make([]crond.Entry, len(schedules)) for i, event := range schedules { - entries[i] = crond.NewEntry(event, j.config.Configfile(), j.config.Title(), j.config.SubTitle(), j.config.Command()+" "+strings.Join(j.config.Arguments(), " ")) + entries[i] = crond.NewEntry( + event, + j.config.Configfile(), + j.config.Title(), + j.config.SubTitle(), + j.config.Command()+" "+strings.Join(j.config.Arguments(), " "), + j.config.WorkingDirectory(), + ) } crontab := crond.NewCrontab(entries) err := crontab.Rewrite() @@ -29,7 +36,14 @@ func (j *Job) createCrondJob(schedules []*calendar.Event) error { func (j *Job) removeCrondJob() error { entries := []crond.Entry{ - crond.NewEntry(calendar.NewEvent(), j.config.Configfile(), j.config.Title(), j.config.SubTitle(), j.config.Command()+" "+strings.Join(j.config.Arguments(), " ")), + crond.NewEntry( + calendar.NewEvent(), + j.config.Configfile(), + j.config.Title(), + j.config.SubTitle(), + j.config.Command()+" "+strings.Join(j.config.Arguments(), " "), + j.config.WorkingDirectory(), + ), } crontab := crond.NewCrontab(entries) err := crontab.Remove()