/
config.go
82 lines (75 loc) · 2.22 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
package main
import (
"fmt"
"strings"
"github.com/spf13/pflag"
)
// Config is the configuration for the sticky router service.
type Config struct {
SubjectPattern string
Queue string
Workers int
subject string
subjectTokens int
durationTokenIdx int
hashTokenIdx int
}
// DefaultConfig is the default configuration for the sticky router service.
func DefaultConfig() *Config {
return &Config{
SubjectPattern: "sticky.route.{duration}.{hash}",
Queue: "default",
Workers: 1,
}
}
// Flags returns a flagset that can be added to the command line.
func (c *Config) Flags(prefix string, defaults *Config) *pflag.FlagSet {
var flags pflag.FlagSet
if defaults == nil {
defaults = DefaultConfig()
}
flags.StringVar(&c.SubjectPattern, prefix+"subject", defaults.SubjectPattern, "Subject pattern to subscribe to")
flags.StringVar(&c.Queue, prefix+"queue", defaults.Queue, "Queue to use when subscribing")
flags.IntVar(&c.Workers, prefix+"workers", defaults.Workers, "Number of workers")
return &flags
}
func (c *Config) parseSubjectPattern() error {
if strings.ContainsAny(c.SubjectPattern, " \t\r\n") {
return fmt.Errorf("subject pattern %q contains whitespace", c.SubjectPattern)
}
tokens := strings.Split(c.SubjectPattern, ".")
c.subjectTokens = len(tokens)
var (
subjectTokens = make([]string, 0, len(tokens))
gotDuration bool
gotHash bool
)
for i, token := range tokens {
switch token {
case "{duration}":
if gotDuration {
return fmt.Errorf("subject pattern %q contains more than one {duration}", c.SubjectPattern)
}
gotDuration = true
c.durationTokenIdx = i
subjectTokens = append(subjectTokens, "*")
case "{hash}":
if gotHash {
return fmt.Errorf("subject pattern %q contains more than one {hash}", c.SubjectPattern)
}
gotHash = true
c.hashTokenIdx = i
subjectTokens = append(subjectTokens, "*")
default:
subjectTokens = append(subjectTokens, token)
}
}
if !gotDuration {
return fmt.Errorf("subject pattern %q does not contain {duration}", c.SubjectPattern)
}
if !gotHash {
return fmt.Errorf("subject pattern %q does not contain {hash}", c.SubjectPattern)
}
c.subject = strings.Join(subjectTokens, ".")
return nil
}