-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
50 lines (42 loc) · 931 Bytes
/
util.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
package internal
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
const (
KibiByte uint64 = 1024
MebiByte = 1024 * KibiByte
GibiByte = 1024 * MebiByte
TebiByte = 1024 * GibiByte
)
var (
reThreshold = regexp.MustCompile(`(?i)^(\d+)\s*([TGMK]i?B)?$`)
ErrThresholdInvalid = errors.New("threshold invalid")
)
func ParseThreshold(threshold string) (uint64, error) {
match := reThreshold.FindStringSubmatch(threshold)
if match == nil {
return 0, ErrThresholdInvalid
}
value, err := strconv.ParseUint(match[1], 10, 64)
if err != nil {
return 0, err
}
var level uint64
switch u := strings.ToLower(match[2]); u {
case "kb", "kib":
level = value * KibiByte
case "", "mb", "mib":
level = value * MebiByte
case "gb", "gib":
level = value * GibiByte
case "tb", "tib":
level = value * TebiByte
default:
return 0, fmt.Errorf("invalid unit")
}
return level, nil
}