This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
forked from streamingfast/sf-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
84 lines (65 loc) · 1.56 KB
/
flags.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
package sftools
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/spf13/viper"
)
var Flags = &flags{}
type flags struct {
}
func (*flags) GetBlockRange(flagName string) (out BlockRange, err error) {
stringRange := viper.GetString(flagName)
if stringRange == "" {
return
}
rawRanges := strings.Split(stringRange, ",")
if len(rawRanges) == 0 {
return
}
if len(rawRanges) > 1 {
return out, fmt.Errorf("accepting a single range for now, got %d", len(rawRanges))
}
out, err = decodeRange(rawRanges[0])
if err != nil {
return out, fmt.Errorf("decode range: %w", err)
}
return
}
func decodeRanges(rawRanges string) (out []BlockRange, err error) {
for _, rawRange := range strings.Split(rawRanges, ",") {
blockRange, err := decodeRange(rawRange)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
out = append(out, blockRange)
}
return
}
func decodeRange(rawRange string) (out BlockRange, err error) {
parts := strings.SplitN(rawRange, ":", 2)
if len(parts) != 2 {
return out, fmt.Errorf("invalid range %q, not matching format `<start>:<end>`", rawRange)
}
out.Start, err = decodeBlockNum("start", parts[0])
if err != nil {
return
}
out.Stop, err = decodeBlockNum("end", parts[1])
if err != nil {
return
}
return
}
func decodeBlockNum(tag string, part string) (out uint64, err error) {
trimmedValue := strings.Trim(part, " ")
if trimmedValue != "" {
out, err = strconv.ParseUint(trimmedValue, 10, 64)
if err != nil {
return out, fmt.Errorf("`<%s>` value %q is not a valid integer", tag, part)
}
}
return
}