/
common.go
134 lines (115 loc) · 3.53 KB
/
common.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cmd
/*
Copyright 2017 - 2022 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"encoding/json"
"fmt"
"reflect"
)
// unitType is used to group together the unit types
type unitType int
// values for the headings
const (
headingCapacity = "CAPACITY"
headingCluster = "CLUSTER"
headingClusterIP = "CLUSTER IP"
headingErrorMessage = "ERROR"
headingExpires = "EXPIRES"
headingExternalIP = "EXTERNAL IP"
headingInstance = "INSTANCE"
headingPassword = "PASSWORD"
headingPercentUsed = "% USED"
headingPod = "POD"
headingPVC = "PVC"
headingService = "SERVICE"
headingStatus = "STATUS"
headingPVCType = "TYPE"
headingUsed = "USED"
headingUsername = "USERNAME"
)
// unitSize recommends the unit we will use to size things
const unitSize = 1024
// the collection of unittypes, from byte to yottabyte
const (
unitB unitType = iota
unitKB
unitMB
unitGB
unitTB
unitPB
unitEB
unitZB
unitYB
)
// unitTypeToString converts the unit types to strings
var unitTypeToString = map[unitType]string{
unitB: "B",
unitKB: "KiB",
unitMB: "MiB",
unitGB: "GiB",
unitTB: "TiB",
unitPB: "PiB",
unitEB: "EiB",
unitZB: "ZiB",
unitYB: "YiB",
}
// getHeaderLength returns the length of any value in a list, so that
// the maximum length of the header can be determined
func getHeaderLength(value interface{}, fieldName string) int {
// get the field from the reflection
r := reflect.ValueOf(value)
field := reflect.Indirect(r).FieldByName(fieldName)
return len(field.String())
}
// getMaxLength returns the maxLength of the strings of a particular value in
// the struct. Increases the max length by 1 to include a buffer
func getMaxLength(results []interface{}, title, fieldName string) int {
maxLength := len(title)
for _, result := range results {
length := getHeaderLength(result, fieldName)
if length > maxLength {
maxLength = length
}
}
return maxLength + 1
}
// getSizeAndUnit determines the best size to return based on the best unit
// where unit is KB, MB, GB, etc...
func getSizeAndUnit(size int64) (float64, unitType) {
// set the unit
var unit unitType
// iterate through each tier, which we will initialize as "bytes"
normalizedSize := float64(size)
// We keep dividing by "unitSize" which is 1024. Once it is less than the unit
// size, or really, once it's less than "1000" of that unit size, that is
// normalized unit we will use.
//
// of course, eventually this will get too big...so bail after yotta bytes
for unit = unitB; normalizedSize > 1000 && unit < unitYB; unit++ {
normalizedSize /= unitSize
}
return normalizedSize, unit
}
// getUnitString maps the raw value of the unit to its corresponding
// abbreviation
func getUnitString(unit unitType) string {
return unitTypeToString[unit]
}
// printJSON renders a JSON response
func printJSON(response interface{}) {
if content, err := json.MarshalIndent(response, "", " "); err != nil {
fmt.Printf(`{"error": "%s"}`, err.Error())
} else {
fmt.Println(string(content))
}
}