-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
80 lines (69 loc) · 1.8 KB
/
config.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package jobsrunner
// TODO: Hot config reload.
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
)
// Config is jobrunner application configuration.
type Config struct {
// Version is used for future incompatible config changes.
Version int `json:"version"`
// Jobs list to run.
Jobs []ConfigJob `json:"jobs"`
}
func NewConfigFromFile(filename string) (Config, error) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
return Config{}, fmt.Errorf("can't read config: %s", err)
}
c := Config{}
if err := json.Unmarshal(contents, &c); err != nil {
return Config{}, fmt.Errorf("bad config file: %s", err)
}
if c.Version != 1 {
return Config{}, fmt.Errorf("unsupported version: %d", c.Version)
}
return c, nil
}
// ConfigJob is a single job to run with the given interval.
type ConfigJob struct {
Cmd string `json:"cmd"`
Interval ConfigJobInterval `json:"interval"`
}
// ConfigJobInterval is a parsed interval.
type ConfigJobInterval int64
// json.Unmarshaler implementation.
func (c *ConfigJobInterval) UnmarshalJSON(vByte []byte) error {
v := strings.Trim(string(vByte), `"`)
// no-op by convention
if v == "null" {
return nil
}
vals := strings.Split(v, " ")
if len(vals) != 2 {
return fmt.Errorf("too many spaces")
}
n, err := strconv.ParseInt(vals[0], 10, 32)
if err != nil {
return fmt.Errorf("can't parse number")
}
if n <= 0 {
return fmt.Errorf("number must be positive")
}
modifier := vals[1]
switch modifier {
case "second", "seconds":
*c = ConfigJobInterval(n * int64(time.Second))
case "minute", "minutes":
*c = ConfigJobInterval(n * int64(time.Minute))
case "hour", "hours":
*c = ConfigJobInterval(n * int64(time.Hour))
default:
return fmt.Errorf("modifier must be one of: seconds, minutes, hours")
}
return nil
}