-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (55 loc) · 1019 Bytes
/
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
56
57
58
59
60
61
62
63
64
65
package main
import (
"github.com/robfig/cron"
"github.com/takaishi/clon/config"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"time"
)
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: "/etc/clon.yml",
},
}
app.Action = func(c *cli.Context) error {
return action(c)
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func action(c *cli.Context) error {
cfg, err := readConfig(c.String("config"))
if err != nil {
return err
}
l, err := time.LoadLocation("Local")
if err != nil {
return err
}
server := cron.NewWithLocation(l)
for _, job := range cfg.Jobs {
server.AddJob(job.Schedule, job)
}
server.Run()
return nil
}
func readConfig(configPath string) (config.Config, error) {
var cfg config.Config
data, err := ioutil.ReadFile(configPath)
if err != nil {
return cfg, err
}
err = yaml.Unmarshal([]byte(data), &cfg)
if err != nil {
return cfg, err
}
return cfg, nil
}