forked from j-keck/lsleases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
63 lines (53 loc) · 1.06 KB
/
utils.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
package main
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
"time"
)
func logOnError(err error, msg ...string) {
if err != nil {
log.Println(msg, ": ", err)
}
}
func exitOnError(err error, msg ...string) {
if err != nil {
log.Println(msg, ": ", err)
os.Exit(1)
}
}
func panicOnError(err error, msg ...string) {
if err != nil {
panic(fmt.Sprintf("%s: %s", msg, err))
}
}
func parseDuration(str string) (time.Duration, error) {
r, err := regexp.Compile("^(\\d+)d(.*)?$")
if err != nil {
return 0, err
}
if r.MatchString(str) {
found := r.FindStringSubmatch(str)
daysStr := found[1]
restStr := found[2]
days, err := strconv.Atoi(daysStr)
if err != nil {
return 0, err
}
daysDuration, err := time.ParseDuration(fmt.Sprintf("%dh", days*24))
if err != nil {
return 0, err
}
var restDuration time.Duration
if len(restStr) > 0 {
restDuration, err = time.ParseDuration(restStr)
if err != nil {
return 0, err
}
}
return time.Duration(int64(daysDuration) + int64(restDuration)), nil
}
return time.ParseDuration(str)
}