forked from benmanns/goworker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queues_flag.go
59 lines (49 loc) · 1.01 KB
/
queues_flag.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
package goworker
import (
"errors"
"fmt"
"strconv"
"strings"
)
var (
errorEmptyQueues = errors.New("You must specify at least one queue.")
errorNonNumericWeight = errors.New("The weight must be a numeric value.")
)
type queuesFlag []string
func (q *queuesFlag) Set(value string) error {
for _, queueAndWeight := range strings.Split(value, ",") {
if queueAndWeight == "" {
continue
}
queue, weight, err := parseQueueAndWeight(queueAndWeight)
if err != nil {
return err
}
for i := 0; i < weight; i++ {
*q = append(*q, queue)
}
}
if len(*q) == 0 {
return errorEmptyQueues
}
return nil
}
func (q *queuesFlag) String() string {
return fmt.Sprint(*q)
}
func parseQueueAndWeight(queueAndWeight string) (queue string, weight int, err error) {
parts := strings.SplitN(queueAndWeight, "=", 2)
queue = parts[0]
if queue == "" {
return
}
if len(parts) == 1 {
weight = 1
} else {
weight, err = strconv.Atoi(parts[1])
if err != nil {
err = errorNonNumericWeight
}
}
return
}