-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
125 lines (103 loc) · 2.98 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package sqsjkr
import (
"fmt"
"github.com/kayac/go-config"
)
// Config is the sqsjkr config
type Config struct {
Account AccountSection `toml:"account"`
Kicker KickerSection `toml:"kicker"`
SQS SQSSection `toml:"sqs"`
}
// AccountSection is aws account information
type AccountSection struct {
Profile string `toml:"profile"`
ID string `toml:"id"`
Region string `toml:"region"`
}
// KickerSection is the config of command kicker
type KickerSection struct {
MaxConcurrentNum int `toml:"max_concurrent_num"`
Trigger string `toml:"life_time_trigger"`
StatsPort int `toml:"stats_port"`
StatsSocket string `toml:"stats_socket"`
}
// SQSSection is the AWS SQS configure
type SQSSection struct {
QueueName string `toml:"queue_name"`
}
// NewConfig create sqsjkr config
func NewConfig() *Config {
return &Config{
Account: AccountSection{},
Kicker: KickerSection{},
SQS: SQSSection{},
}
}
// SetAWSAccount set aws account
func (c *Config) SetAWSAccount(id, profile, region string) {
c.Account.ID = id
c.Account.Region = region
c.Account.Profile = profile
}
// SetSQSQueue set sqs queue name
func (c *Config) SetSQSQueue(qname string) {
c.SQS.QueueName = qname
}
// SetKickerConfig set kicker config
func (c *Config) SetKickerConfig(num int, trigger string) {
c.Kicker.MaxConcurrentNum = num
c.Kicker.Trigger = trigger
}
// SetConcurrentNum set number of concurrent to execute job
func (c *Config) SetConcurrentNum(num int) {
c.Kicker.MaxConcurrentNum = num
}
// SetTriggerCommand set trigger command
func (c *Config) SetTriggerCommand(trigger string) {
c.Kicker.Trigger = trigger
}
// SetStatsPort set stats api port number
func (c *Config) SetStatsPort(port int) error {
if c.Kicker.StatsSocket != "" {
return fmt.Errorf("Already set stats api unix domain socket. if you use tcp listener, unset StatsSocket.")
}
c.Kicker.StatsPort = port
return nil
}
// SetStatsSocket set unix domain socket path
func (c *Config) SetStatsSocket(sock string) error {
if c.Kicker.StatsPort != 0 {
return fmt.Errorf("Already set stats api port number. if you use unix domain socket, unset StatsPort.")
}
c.Kicker.StatsSocket = sock
return nil
}
// LoadConfig loads config file by config file path
func LoadConfig(path string) (*Config, error) {
var conf Config
if err := config.LoadWithEnvTOML(&conf, path); err != nil {
return nil, err
}
// Set Default Value if null
if conf.Kicker.MaxConcurrentNum == 0 {
conf.Kicker.MaxConcurrentNum = DefaultMaxCocurrentNum
}
return &conf, (&conf).Validate()
}
// Validate config validation
func (c *Config) Validate() error {
if c.SQS.QueueName == "" {
return fmt.Errorf("queue_name is required")
}
if c.Account.ID == "" {
return fmt.Errorf("aws account id is required")
}
if c.Account.Region == "" {
return fmt.Errorf("aws region is required")
}
if c.Kicker.StatsPort != 0 && c.Kicker.StatsSocket != "" {
return fmt.Errorf("could not specify both stats api port and unix domain socket")
}
return nil
}