/
listformatters.go
168 lines (148 loc) · 3.58 KB
/
listformatters.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package storage
import (
"fmt"
"io"
"sort"
"strconv"
"strings"
"github.com/juju/juju/cmd/output"
)
// formatListTabular writes a tabular summary of storage instances.
func formatStorageListTabular(writer io.Writer, storageInfo map[string]StorageInfo) error {
tw := output.TabWriter(writer)
p := func(values ...interface{}) {
for _, v := range values {
fmt.Fprintf(tw, "%v\t", v)
}
fmt.Fprintln(tw)
}
p("[Storage]")
p("UNIT\tID\tLOCATION\tSTATUS\tMESSAGE")
byUnit := make(map[string]map[string]storageAttachmentInfo)
for storageId, storageInfo := range storageInfo {
if storageInfo.Attachments == nil {
byStorage := byUnit[""]
if byStorage == nil {
byStorage = make(map[string]storageAttachmentInfo)
byUnit[""] = byStorage
}
byStorage[storageId] = storageAttachmentInfo{
storageId: storageId,
kind: storageInfo.Kind,
persistent: storageInfo.Persistent,
status: storageInfo.Status,
}
continue
}
for unitId, a := range storageInfo.Attachments.Units {
byStorage := byUnit[unitId]
if byStorage == nil {
byStorage = make(map[string]storageAttachmentInfo)
byUnit[unitId] = byStorage
}
byStorage[storageId] = storageAttachmentInfo{
storageId: storageId,
unitId: unitId,
kind: storageInfo.Kind,
persistent: storageInfo.Persistent,
location: a.Location,
status: storageInfo.Status,
}
}
}
// First sort by units
units := make([]string, 0, len(storageInfo))
for unit := range byUnit {
units = append(units, unit)
}
sort.Strings(slashSeparatedIds(units))
for _, unit := range units {
// Then sort by storage ids
byStorage := byUnit[unit]
storageIds := make([]string, 0, len(byStorage))
for storageId := range byStorage {
storageIds = append(storageIds, storageId)
}
sort.Strings(slashSeparatedIds(storageIds))
for _, storageId := range storageIds {
info := byStorage[storageId]
p(info.unitId, info.storageId, info.location, info.status.Current, info.status.Message)
}
}
tw.Flush()
return nil
}
type storageAttachmentInfo struct {
storageId string
unitId string
kind string
persistent bool
location string
status EntityStatus
}
type slashSeparatedIds []string
func (s slashSeparatedIds) Len() int {
return len(s)
}
func (s slashSeparatedIds) Swap(a, b int) {
s[a], s[b] = s[b], s[a]
}
func (s slashSeparatedIds) Less(a, b int) bool {
return compareSlashSeparated(s[a], s[b]) == -1
}
// compareSlashSeparated compares a with b, first the string before
// "/", and then the integer or string after. Empty strings are sorted
// after all others.
func compareSlashSeparated(a, b string) int {
switch {
case a == "" && b == "":
return 0
case a == "":
return 1
case b == "":
return -1
}
sa := strings.SplitN(a, "/", 2)
sb := strings.SplitN(b, "/", 2)
if sa[0] < sb[0] {
return -1
}
if sa[0] > sb[0] {
return 1
}
getInt := func(suffix string) (bool, int) {
num, err := strconv.Atoi(suffix)
if err != nil {
return false, 0
}
return true, num
}
naIsNumeric, na := getInt(sa[1])
if !naIsNumeric {
return compareStrings(sa[1], sb[1])
}
nbIsNumeric, nb := getInt(sb[1])
if !nbIsNumeric {
return compareStrings(sa[1], sb[1])
}
switch {
case na < nb:
return -1
case na == nb:
return 0
}
return 1
}
// compareStrings does what strings.Compare does, but without using
// strings.Compare as it does not exist in Go 1.2.
func compareStrings(a, b string) int {
if a == b {
return 0
}
if a < b {
return -1
}
return 1
}