-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
75 lines (64 loc) · 1.38 KB
/
info.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
package ilo
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
// Info is a struct to store ILO Info
type Info struct {
Host string
Serial string
Model string
ILOVersion string
Firmware string
Success bool
}
func (i Info) String() string {
return fmt.Sprintf("%s | %s | %s | %s | %s", i.Host, i.Serial, i.Model, i.ILOVersion, i.Firmware)
}
func cmpIP(ip1, ip2 string) bool {
octets1 := strings.Split(ip1, ".")
octets2 := strings.Split(ip2, ".")
for i := range octets1 {
o1, _ := strconv.Atoi(octets1[i])
o2, _ := strconv.Atoi(octets2[i])
switch {
case o1 < o2:
return true
case o2 < o1:
return false
}
}
return false
}
// ByHost implements sort.Interface for []Info
type ByHost []Info
func (h ByHost) Len() int { return len(h) }
func (h ByHost) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h ByHost) Less(i, j int) bool { return cmpIP(h[i].Host, h[j].Host) }
// PrintILOTable takes in a list of ILO infos and prints a table for them.
func PrintILOTable(infos []Info) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeader(
[]string{
"IP",
"Serial",
"Model",
"ILO Version",
"Firmware Version",
},
)
for _, i := range infos {
table.Append([]string{
i.Host,
i.Serial,
i.Model,
i.ILOVersion,
i.Firmware,
})
}
table.Render()
}