-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortabletags.go
51 lines (44 loc) · 1.04 KB
/
sortabletags.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
package report
import (
"bytes"
"sort"
"strings"
"github.com/aws/aws-sdk-go/service/ec2"
)
type SortableTags struct {
Tags map[string]string
Keys []string
}
func NewSortableTags() *SortableTags {
return &SortableTags{Tags: make(map[string]string)}
}
func (st *SortableTags) Add(tags []*ec2.Tag) {
for _, tag := range tags {
st.Keys = append(st.Keys, *tag.Key)
st.Tags[*tag.Key] = *tag.Value
}
less := func(i, j int) bool {
return strings.ToLower(st.Keys[i]) < strings.ToLower(st.Keys[j])
}
sort.Slice(st.Keys, less)
}
func (st *SortableTags) ToTableData() string {
n := len(st.Keys)
if n == 0 {
return ""
}
var buffer bytes.Buffer
for _, key := range st.Keys[:n-1] {
maxWidth := 50
if len(st.Tags[key]+key) > maxWidth-1 {
i := maxWidth - 1 - len(key)
for i < len(st.Tags[key]) {
st.Tags[key] = st.Tags[key][:i] + "\n " + st.Tags[key][i:]
i += maxWidth - 2
}
}
buffer.WriteString(key + ":" + st.Tags[key] + "\n")
}
buffer.WriteString(st.Keys[n-1] + ":" + st.Tags[st.Keys[n-1]])
return buffer.String()
}