|
package main |
|
|
|
import ( |
|
"time" |
|
"encoding/json" |
|
"fmt" |
|
"io/ioutil" |
|
"os" |
|
"os/exec" |
|
"strings" |
|
) |
|
|
|
type Config struct { |
|
Alarms []struct { |
|
Name string |
|
Command string |
|
Arguments []string |
|
Match struct { |
|
Day []int |
|
Month []int |
|
Year []int |
|
Second []int |
|
Minute []int |
|
Hour []int |
|
Weekday []int |
|
} |
|
Modulo struct { |
|
Day int |
|
Month int |
|
Year int |
|
Second int |
|
Minute int |
|
Hour int |
|
Weekday int |
|
} |
|
} |
|
} |
|
|
|
var config Config |
|
|
|
func init() { |
|
file, e := ioutil.ReadFile("./config.json") |
|
if e != nil { |
|
fmt.Printf("Config error: %v\n", e) |
|
os.Exit(1) |
|
} |
|
json.Unmarshal(file, &config) |
|
fmt.Printf("Loaded config: %v\n", config) |
|
} |
|
|
|
func main() { |
|
for { |
|
now := time.Now() |
|
for _,v := range(config.Alarms) { |
|
if (len(v.Match.Day)>0 && !canFind(v.Match.Day, now.Day())) || |
|
(len(v.Match.Month)>0 && !canFind(v.Match.Month, int(now.Month()))) || |
|
(len(v.Match.Year)>0 && !canFind(v.Match.Year, now.Year())) || |
|
(len(v.Match.Second)>0 && !canFind(v.Match.Second, now.Second())) || |
|
(len(v.Match.Minute)>0 && !canFind(v.Match.Minute, now.Minute())) || |
|
(len(v.Match.Hour)>0 && !canFind(v.Match.Hour, now.Hour())) || |
|
(len(v.Match.Weekday)>0&&!canFind(v.Match.Weekday,int(now.Weekday())))|| |
|
(v.Modulo.Day != 0 && now.Day()%v.Modulo.Day != 0) || |
|
(v.Modulo.Month != 0 && int(now.Month())%v.Modulo.Month != 0) || |
|
(v.Modulo.Year != 0 && now.Year()%v.Modulo.Year != 0) || |
|
(v.Modulo.Second != 0 && now.Second()%v.Modulo.Second != 0) || |
|
(v.Modulo.Minute != 0 && now.Minute()%v.Modulo.Minute != 0) || |
|
(v.Modulo.Hour != 0 && now.Hour()%v.Modulo.Hour != 0) || |
|
(v.Modulo.Weekday != 0 && int(now.Weekday())%v.Modulo.Weekday != 0) { |
|
continue |
|
} else { |
|
fmt.Printf("%s: %s %s\n", v.Name, v.Command, v.Arguments) |
|
go func() { |
|
shell := exec.Command(v.Command,v.Arguments...) |
|
out, _ := shell.CombinedOutput() |
|
lines := strings.Split(string(out), "\n") |
|
for i, line := range(lines) { |
|
if len(line) != 0 { |
|
fmt.Printf("%d: %s\n",i, line) |
|
} |
|
} |
|
}() |
|
} |
|
} |
|
time.Sleep(time.Second) |
|
} |
|
} |
|
func canFind(x []int, y int) bool { |
|
for _,z := range(x) { if y == z { return true } } |
|
return false |
|
} |