This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
112 lines (100 loc) · 2.06 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"encoding/json"
"errors"
"os"
"strconv"
"strings"
"layeh.com/murmur-cli/MurmurRPC"
"github.com/golang/protobuf/proto"
)
func Output(data interface{}, err error) {
if err != nil {
panic(err)
}
if outputTemplate != nil {
if err := outputTemplate.Execute(os.Stdout, data); err != nil {
panic(err)
}
return
}
encoder := json.NewEncoder(os.Stdout)
if err := encoder.Encode(data); err != nil {
panic(err)
}
}
type Args []string
func (a Args) MustServer(i int) *MurmurRPC.Server {
if len(a) <= i {
panic(errors.New("missing server ID argument"))
}
id, err := strconv.Atoi(a[i])
if err != nil {
panic(errors.New("invalid server ID"))
}
return &MurmurRPC.Server{
Id: proto.Uint32(uint32(id)),
}
}
func (a Args) MustString(i int) string {
if len(a) <= i {
panic(errors.New("missing string value"))
}
return a[i]
}
func (a Args) String(i int) (string, bool) {
if len(a) <= i {
return "", false
}
return a[i], true
}
func (a Args) MustBitmask(i int, values map[string]int32, allowEmpty bool) int32 {
if len(a) <= i {
panic(errors.New("missing bitmask value"))
}
var val int32
for _, item := range strings.Split(a[i], ",") {
itemVal, ok := values[item]
if !ok {
panic(errors.New("invalid bitmask value"))
}
val |= itemVal
}
if !allowEmpty && val == 0 {
panic(errors.New("empty bitmask value"))
}
return val
}
func (a Args) MustUint32(i int) uint32 {
if len(a) <= i {
panic(errors.New("missing uint32 value"))
}
n, err := strconv.ParseUint(a[i], 10, 32)
if err != nil {
panic(err)
}
return uint32(n)
}
func (a Args) Uint32(i int) (uint32, bool) {
if len(a) <= i {
return 0, false
}
n, err := strconv.ParseUint(a[i], 10, 32)
if err != nil {
return 0, false
}
return uint32(n), true
}
func (a Args) PrefixedUint32(prefix string, i int) (uint32, bool) {
if len(a) <= i {
panic(errors.New("missing prefixed uint32 value"))
}
if !strings.HasPrefix(a[i], prefix) {
return 0, false
}
n, err := strconv.ParseUint(a[i][len(prefix):], 10, 32)
if err != nil {
panic(err)
}
return uint32(n), true
}