-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
69 lines (55 loc) · 1.13 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
package main
import (
"errors"
"fmt"
"io/ioutil"
"time"
"gopkg.in/yaml.v3"
)
type (
Config struct {
NSQAdmin string `yaml:"nsqadmin"`
Worker int `yaml:"worker"`
Timeout time.Duration `yaml:"timeout"`
Action string `yaml:"action"`
Target []string `yaml:"target"`
}
)
const (
defaultTimeout = 2 * time.Second
)
func readConfig(file string) (Config, error) {
var conf Config
data, err := ioutil.ReadFile(file)
if err != nil {
return conf, err
}
if err := yaml.Unmarshal(data, &conf); err != nil {
return conf, err
}
return conf, nil
}
func (c *Config) validate() error {
if c.NSQAdmin == "" {
return errors.New("nsqadmin can not be empty.")
}
if c.Action == "" {
return errors.New("action can not be empty.")
}
if c.Timeout == 0 {
c.Timeout = defaultTimeout
}
if len(c.Target) == 0 {
return errors.New("topic / channel can not be empty.")
}
// Unlimited workerpool
if c.Worker <= 0 {
c.Worker = len(c.Target)
}
_, err := c.nsqInfo(fmt.Sprintf("/api/topics/%s", c.Target[0]))
if err != nil {
return err
}
fmt.Println("nsqpause is ready.")
return nil
}