-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
55 lines (48 loc) · 1.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import "encoding/json"
import "io/ioutil"
import "log"
import "flag"
import "./lib"
var configLocation = flag.String("config", "config.json", "Sets the location and name of the config file")
func main() {
flag.Parse()
log.Println("Local-IFTTT Starting...")
log.Println("Reading task config")
configContents := readConfig()
tasks := createTasks(configContents)
schedule(tasks)
}
func schedule(tasks []lib.Task) {
allTasksDone := make(chan bool, len(tasks))
for _, task := range tasks {
log.Println(task.Name, "is starting...")
go func(t lib.Task, scheduleDone chan bool) {
for true {
taskDone := make(chan bool)
go t.Run(taskDone)
<-taskDone
log.Println(t.Name, "is done, restarting...")
}
scheduleDone <- true
}(task, allTasksDone)
}
for i := range allTasksDone {
log.Panic("All tasks should never complete: ", i)
}
}
func readConfig() []byte {
contents, err := ioutil.ReadFile(*configLocation)
if err != nil {
log.Panic("Error reading config.json: ", err)
}
return contents
}
func createTasks(rawTasks []byte) []lib.Task {
var tasks []lib.Task
err := json.Unmarshal(rawTasks, &tasks)
if err != nil {
log.Panic("Unable to unmarshal tasks", err)
}
return tasks
}