summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go42
1 files changed, 27 insertions, 15 deletions
diff --git a/main.go b/main.go
index 2674b07..f837340 100644
--- a/main.go
+++ b/main.go
@@ -17,12 +17,20 @@ import (
"github.com/robfig/cron/v3"
)
-type inputLine struct {
- Schedule string
- Task string
+type TaskAddingJob struct {
+ Schedule cron.Schedule
+ Task cron.Job
}
-var lastSeenInput = map[inputLine]struct{}{}
+type Task struct {
+ Name string
+}
+
+func (t Task) Run() {
+ createTask(t.Name)
+}
+
+var lastSeenInput = map[TaskAddingJob]struct{}{}
var todoistToken string
var httpClient = http.Client{
Timeout: 10 * time.Second,
@@ -91,8 +99,8 @@ func inputFilePath() (string, error) {
var inputFileRe = regexp.MustCompile(`([^\s]+\s+[^\s]+\s+[^\s]+\s+[^\s]+\s+[^\s]+)\s+(.*)`)
-func readInput(p string) (map[inputLine]struct{}, error) {
- input := map[inputLine]struct{}{}
+func readInput(p string) (map[TaskAddingJob]struct{}, error) {
+ input := map[TaskAddingJob]struct{}{}
f, err := os.Open(p)
if err != nil {
@@ -110,9 +118,17 @@ func readInput(p string) (map[inputLine]struct{}, error) {
matches := inputFileRe.FindStringSubmatch(line)
if matches == nil || len(matches) != 1+2 {
log.Printf("failed to parse input line: '%s'", line)
+ continue
}
- input[inputLine{Schedule: matches[1], Task: matches[2]}] = struct{}{}
+ schedule, err := cron.ParseStandard(matches[1])
+ if err != nil {
+ log.Printf("Failed to add '%s' with recurrence '%s': %s", matches[2], matches[1], err)
+ continue
+ }
+ job := Task{Name: matches[2]}
+
+ input[TaskAddingJob{Schedule: schedule, Task: job}] = struct{}{}
}
if err := scanner.Err(); err != nil {
@@ -122,7 +138,7 @@ func readInput(p string) (map[inputLine]struct{}, error) {
return input, nil
}
-func inputChanged(old, new map[inputLine]struct{}) bool {
+func inputChanged(old, new map[TaskAddingJob]struct{}) bool {
if len(new) != len(old) {
return true
}
@@ -167,13 +183,9 @@ func createTask(task string) {
}
}
-func addJobs(c *cron.Cron, tasks map[inputLine]struct{}) {
+func addJobs(c *cron.Cron, tasks map[TaskAddingJob]struct{}) {
for task := range tasks {
- _, err := c.AddFunc(task.Schedule, func() { createTask(task.Task) })
- if err != nil {
- log.Printf("Failed to add '%s' with recurrence '%s': %s", task.Task, task.Schedule, err)
- } else {
- log.Printf("Added '%s' with recurrence '%s'", task.Task, task.Schedule)
- }
+ _ = c.Schedule(task.Schedule, task.Task)
}
+ log.Printf("Added %d jobs", len(tasks))
}