-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
56 lines (45 loc) · 1.09 KB
/
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
51
52
53
54
55
56
package main
import (
"fmt"
"math/rand"
)
var randAlphabet = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// CommandError is the checked exception thrown on runtime errors
type CommandError struct {
msg string // description of error
err error // inner error
}
func (e *CommandError) Error() string { return e.msg }
// Panics with a message if the given error isn't nil
func check(err error, a ...interface{}) {
if err != nil {
var msg string
if len(a) > 0 {
msg = fmt.Sprintf("%s (%s)", fmt.Sprintf(a[0].(string), a[1:]...), err)
} else {
msg = fmt.Sprintf("%s", err)
}
panic(&CommandError{msg, err})
}
}
// Panics with a message if the given condition isn't true
func assertThat(condition bool, msg string, a ...interface{}) {
if !condition {
panic(&CommandError{fmt.Sprintf(msg, a...), nil})
}
}
func defaults(a ...string) string {
for _, item := range a {
if len(item) > 0 {
return item
}
}
return ""
}
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = randAlphabet[rand.Intn(len(randAlphabet))]
}
return string(b)
}