Skip to content

Commit 5b01351

Browse files
committed
Enabled use of @ instead of * in filenames
Added configuration for scan time Files must gave .godoit extention
1 parent 7d863c9 commit 5b01351

6 files changed

Lines changed: 35 additions & 18 deletions

File tree

config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
type GoDoItConfig struct {
1111
Include []string `toml:"include" doc:"Paths to scan"`
1212
JobExecutorScript string`toml:"JobExecutorScript" doc:"Paths for job executor script"`
13+
ScanTime int `toml:"ScanTime" doc:"Scan time in seconds"`
1314
}
1415

1516
func LoadConfig() *GoDoItConfig {
@@ -18,7 +19,8 @@ func LoadConfig() *GoDoItConfig {
1819
}
1920
cfgFile := os.Args[1]
2021
log.Printf("Loading config file: %s", cfgFile)
21-
cfg, err := config.NewConfig(cfgFile, nil)
22+
defaults := GoDoItConfig{[]string{}, "", 30}
23+
cfg, err := config.NewConfig(cfgFile, defaults)
2224
if err != nil {
2325
log.Fatalf("Error loading configuration: %s", err.Error())
2426
}

execution.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ import (
99
type JobExecutor func(jobName, jobPath string)
1010

1111
func JobExecutorFromScript(jobExecutorScript string) JobExecutor {
12-
expandedJobExecutorScript := os.ExpandEnv(jobExecutorScript)
12+
if len(jobExecutorScript) == 0 {
13+
log.Fatalf("Job executor is not defined")
14+
}
15+
jobExecutorScript = os.ExpandEnv(jobExecutorScript)
1316
return func(jobName, jobPath string) {
14-
cmd := exec.Command(expandedJobExecutorScript, jobName, jobPath)
17+
cmd := exec.Command(jobExecutorScript, jobName, jobPath)
18+
log.Printf("Running comand line: %s %s %s", jobExecutorScript, jobName, jobPath)
1519
cmd.Stdout = os.Stdout
1620
cmd.Stderr = os.Stderr
1721
err := cmd.Run()
1822
if err != nil {
19-
log.Printf("ERROR: Failed to execute executor script %s", expandedJobExecutorScript)
23+
log.Printf("ERROR: Failed to execute executor script %s %s %s", jobExecutorScript, jobName, jobPath)
2024
}
2125
}
2226
}

execution_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
)
77

88
func TestExecutor(t *testing.T) {
9-
JobExecutor exec = JobExecutorFromScript("/bin/bash/echo Hello")
10-
exec.
9+
// TODO...
10+
jobExec := JobExecutorFromScript("./test_wrapper.sh")
11+
jobExec("name","path")
1112
assert.True(t, true, "Failed to parse job")
1213
}

job.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ type Job struct {
1212
Name string
1313
}
1414

15-
var cronSpecRegex,_ = regexp.Compile(`\s*($|#|\w+\s*=|(\?|\*|(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?(?:,(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?)*)\s+(\?|\*|(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?(?:,(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?)*)\s+(\?|\*|(?:[01]?\d|2[0-3])(?:(?:-|\/|\,)(?:[01]?\d|2[0-3]))?(?:,(?:[01]?\d|2[0-3])(?:(?:-|\/|\,)(?:[01]?\d|2[0-3]))?)*)\s+(\?|\*|(?:0?[1-9]|[12]\d|3[01])(?:(?:-|\/|\,)(?:0?[1-9]|[12]\d|3[01]))?(?:,(?:0?[1-9]|[12]\d|3[01])(?:(?:-|\/|\,)(?:0?[1-9]|[12]\d|3[01]))?)*)\s+(\?|\*|(?:[1-9]|1[012])(?:(?:-|\/|\,)(?:[1-9]|1[012]))?(?:L|W)?(?:,(?:[1-9]|1[012])(?:(?:-|\/|\,)(?:[1-9]|1[012]))?(?:L|W)?)*|\?|\*|(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?(?: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012]))?(?:L|W)?)*|\@|\*|(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?(?:,(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?)*)\s+(\@|\*|(?:[0-6])(?:(?:-|\/|\,|#)(?:[0-6]))?(?:L)?(?:,(?:[0-6])(?:(?:-|\/|\,|#)(?:[0-6]))?(?:L)?)*|\@|\*|(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?(?:,(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?)*)(|\s)+(\@|\*|(?:|\d{4})(?:(?:-|\/|\,)(?:|\d{4}))?(?:,(?:|\d{4})(?:(?:-|\/|\,)(?:|\d{4}))?)*)) (.*)\.godoit`)
1616

1717
func ParseJobFile(path, filename string) *Job {
1818
// Return commented out files
19-
if strings.HasPrefix(filename, "#") {
19+
if strings.HasPrefix(filename, "--") {
2020
return nil
2121
}
2222
if result := cronSpecRegex.FindStringSubmatch(filename); result != nil {
23-
return &Job{filepath.Join(path, filename), result[1], strings.TrimSpace(result[10])}
23+
return &Job{filepath.Join(path, filename), strings.Replace(result[1], "@", "*", -1), strings.TrimSpace(result[10])}
2424
} else {
2525
return nil
2626
}

job_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,42 @@ import (
66
)
77

88
func TestPathMatching(t *testing.T) {
9-
job := ParseJobFile("a_path", "0 30 * * * * job name ")
9+
job := ParseJobFile("a_path", "0 30 * * * * job name .godoit")
1010
assert.NotNil(t, job, "Failed to parse job")
1111
assert.Equal(t, "0 30 * * * *", job.Spec)
1212
assert.Equal(t, "job name", job.Name)
13-
assert.Equal(t, "a_path/0 30 * * * * job name ", job.Filepath)
13+
assert.Equal(t, "a_path/0 30 * * * * job name .godoit", job.Filepath)
14+
}
15+
16+
func TestPathMatchingWithRepalce(t *testing.T) {
17+
job := ParseJobFile("a_path", "0 30 @ @ @ @ job name.godoit")
18+
assert.NotNil(t, job, "Failed to parse job")
19+
assert.Equal(t, "0 30 * * * *", job.Spec)
20+
assert.Equal(t, "job name", job.Name)
21+
assert.Equal(t, "a_path/0 30 @ @ @ @ job name.godoit", job.Filepath)
1422
}
1523

1624
func TestPathMatching2(t *testing.T) {
17-
job := ParseJobFile("a_path", "* * * * * * TestScanRemoveJob")
25+
job := ParseJobFile("a_path", "* * * * * * TestScanRemoveJob.godoit")
1826
assert.NotNil(t, job, "Failed to parse job")
1927
}
2028

2129
func TestPathAll(t *testing.T) {
22-
job := ParseJobFile("a_path", "* * * * * * job name")
30+
job := ParseJobFile("a_path", "* * * * * * job name.godoit")
2331
assert.NotNil(t, job, "Failed to parse job")
2432
}
2533

2634
func TestCommentedOutJobShouldBeNil(t *testing.T) {
27-
job := ParseJobFile("a_path", "#0 30 * * * * job name")
35+
job := ParseJobFile("a_path", "--0 30 * * * * job name.godoit")
2836
assert.Nil(t, job, "Should be nil")
2937
}
3038

3139
func TestIncompleteTaskShouldBeNil(t *testing.T) {
32-
job := ParseJobFile("a_path", "0 30 * * *")
40+
job := ParseJobFile("a_path", "0 30 * * *.godoit")
3341
assert.Nil(t, job, "Should be nil")
3442
}
3543

3644
func TestWithoutNameShouldBeNil(t *testing.T) {
37-
job := ParseJobFile("a_path", "0 30 * * * *")
45+
job := ParseJobFile("a_path", "0 30 * * * *.godoit")
3846
assert.Nil(t, job, "Should be nil")
3947
}

main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import (
55
"os"
66
"os/signal"
77
"log"
8+
"fmt"
89
)
910

1011

1112
func main() {
1213
log.Println("Starting GoDoIt")
1314

14-
scanner := NewScanner(LoadConfig())
15+
config := LoadConfig()
16+
scanner := NewScanner(config)
1517

1618
cron := cron.New()
17-
cron.AddFunc("@every 5s", func(){scanner.Run()})
19+
cron.AddFunc(fmt.Sprintf("@every %ds",config.ScanTime), func(){scanner.Run()})
1820
log.Println("Starting scanner")
1921
cron.Start()
2022

0 commit comments

Comments
 (0)