-
Notifications
You must be signed in to change notification settings - Fork 199
/
operationmodes.go
75 lines (62 loc) · 2.26 KB
/
operationmodes.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
package operationmodes
import (
"fmt"
"strings"
)
const (
OperationModeFullArchive = "full-archive"
OperationModeDbLookupExtension = "db-lookup-extension"
OperationModeHistoricalBalances = "historical-balances"
OperationModeLiteObserver = "lite-observer"
)
// ParseOperationModes will check and parse the operation modes
func ParseOperationModes(operationModeList string) ([]string, error) {
if len(operationModeList) == 0 {
return []string{}, nil
}
modes := strings.Split(operationModeList, ",")
for _, mode := range modes {
err := checkOperationModeValidity(mode)
if err != nil {
return []string{}, err
}
}
// db lookup extension and historical balances
isInvalid := sliceContainsBothElements(modes, OperationModeHistoricalBalances, OperationModeDbLookupExtension)
if isInvalid {
return []string{}, fmt.Errorf("operation-mode flag cannot contain both db-lookup-extension and historical-balances")
}
// lite observer and historical balances
isInvalid = sliceContainsBothElements(modes, OperationModeLiteObserver, OperationModeHistoricalBalances)
if isInvalid {
return []string{}, fmt.Errorf("operation-mode flag cannot contain both lite-observer and historical-balances")
}
// lite observer and full archive
isInvalid = sliceContainsBothElements(modes, OperationModeLiteObserver, OperationModeFullArchive)
if isInvalid {
return []string{}, fmt.Errorf("operation-mode flag cannot contain both lite-observer and full-archive")
}
return modes, nil
}
func checkOperationModeValidity(mode string) error {
switch mode {
case OperationModeFullArchive, OperationModeDbLookupExtension, OperationModeHistoricalBalances, OperationModeLiteObserver:
return nil
default:
return fmt.Errorf("invalid operation mode <%s>", mode)
}
}
func sliceContainsBothElements(elements []string, first string, second string) bool {
containsFirstElement := SliceContainsElement(elements, first)
containsSecondElement := SliceContainsElement(elements, second)
return containsFirstElement && containsSecondElement
}
// SliceContainsElement will return true if the provided slice contains the provided element
func SliceContainsElement(elements []string, element string) bool {
for _, el := range elements {
if el == element {
return true
}
}
return false
}