Skip to content

Commit

Permalink
add working directory to crontab line
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Jan 18, 2021
1 parent 9d931a3 commit 641dc91
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
17 changes: 14 additions & 3 deletions crond/crontab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions crond/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions crond/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions schedule/schedule_crond.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 641dc91

Please sign in to comment.