-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
87 lines (74 loc) · 2.42 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
package main
import (
"flag"
"os"
"time"
"github.com/e-travel/message-dispatcher/servers"
)
type Config struct {
socketType string
socketAddress string
batchFrequency time.Duration
streamName string
awsRegion string
influxHost string
influxDatabase string
bufferSize int
dispatcherType string
}
var ValidSocketTypes = map[string]bool{
servers.UNIXGRAM: true,
servers.UDP: true,
}
func ParseFromCommandLine(config *Config) {
flag.StringVar(&config.dispatcherType, "dispatcher", "echo", "Dispatcher type (echo, kinesis, influx)")
flag.StringVar(&config.socketType, "type", servers.UNIXGRAM, "The socket's type (unixgram, udp)")
flag.IntVar(&config.bufferSize, "size", 1024, "The size of the message buffer")
flag.DurationVar(&config.batchFrequency, "frequency", 10*time.Second, "The maximum frequency with which data batches are sent to the backend")
flag.StringVar(&config.socketAddress, "address", "/tmp/msg-dsp.sock", "The socket's address (file)")
flag.StringVar(&config.streamName, "stream-name", "", "The name of the kinesis stream")
flag.StringVar(&config.awsRegion, "aws-region", "eu-west-1", "The kinesis stream's AWS region")
flag.StringVar(&config.influxHost, "influx-host", "http://localhost:8086", "Influx server hostname")
flag.StringVar(&config.influxDatabase, "influx-database", "", "Influx database to use")
helpRequested := flag.Bool("help", false, "Print usage help and exit")
if len(os.Args) < 2 {
*helpRequested = true
}
flag.Parse()
if *helpRequested {
flag.Usage()
os.Exit(0)
}
}
func (config *Config) Validate() bool {
switch {
case !validateSocketType(config.socketType):
return false
case !validateStreamName(config.streamName, config.dispatcherType):
return false
case !validateInflux(config.dispatcherType, config.influxHost, config.influxDatabase):
return false
case !validateBatchFrequency(config.batchFrequency):
return false
default:
return true
}
}
func validateSocketType(socketType string) bool {
return ValidSocketTypes[socketType]
}
func validateStreamName(streamName string, dispatcherType string) bool {
if dispatcherType == "kinesis" {
return len(streamName) > 0
}
return true
}
func validateInflux(dispatcherType string, influxHost string, influxDatabase string) bool {
if dispatcherType == "influx" {
return influxHost != "" && influxDatabase != ""
}
return true
}
func validateBatchFrequency(freq time.Duration) bool {
return freq > 0
}