forked from aerospike/aerospike-management-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
99 lines (78 loc) · 1.92 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
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
package deployment
import (
"fmt"
"strconv"
"strings"
as "github.com/aerospike/aerospike-client-go/v7"
)
type InfoResult map[string]string
func (ir InfoResult) toInt(key string) (int, error) {
val, ok := ir[key]
if !ok {
return 0, fmt.Errorf("field %s missing", key)
}
n, err := strconv.Atoi(val)
if err != nil {
return 0, fmt.Errorf("failed to convert key %q to int: %v", key, err)
}
return n, nil
}
func (ir InfoResult) toString(key string) (string, error) {
val, ok := ir[key]
if !ok {
return "", fmt.Errorf("field %s missing", key)
}
return val, nil
}
func (ir InfoResult) toBool(key string) (bool, error) {
val, ok := ir[key]
if !ok {
return false, fmt.Errorf("field %s missing", key)
}
b, err := strconv.ParseBool(val)
if err != nil {
return false, fmt.Errorf("failed to convert key %q to bool: %v", key, err)
}
return b, nil
}
// parseInfo parses the output of an info command
func parseInfo(info map[string]string) map[string]string {
m := make(map[string]string)
for k, v := range info {
if strings.Contains(v, ";") {
all := strings.Split(v, ";")
for _, s := range all {
// TODO: Is it correct, it was crashing in parsing below string
// error-no-data-yet-or-back-too-small;error-no-data-yet-or-back-too-small;
if strings.Contains(s, "=") {
ss := strings.Split(s, "=")
kk, vv := ss[0], ss[1]
m[kk] = vv
} else {
m[k] = v
}
}
} else {
m[k] = v
}
}
return m
}
func getHostIDsFromHostConns(hostConns []*HostConn) []string {
hostIDs := make([]string, 0, len(hostConns))
for _, hc := range hostConns {
hostIDs = append(hostIDs, hc.ID)
}
return hostIDs
}
func getHostsFromHostConns(hostConns []*HostConn, policy *as.ClientPolicy) ([]*host, error) {
hosts := make([]*host, len(hostConns))
for i := range hostConns {
host, err := hostConns[i].toHost(policy)
if err != nil {
return nil, err
}
hosts[i] = host
}
return hosts, nil
}