-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
summary.go
186 lines (165 loc) · 3.99 KB
/
summary.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package ecrm
import (
"encoding/json"
"fmt"
"io"
"log"
"sort"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
ecrTypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/samber/lo"
)
const (
SummaryTypeImage = "Image"
SummaryTypeImageIndex = "Image index"
SummaryTypeSociIndex = "Soci index"
)
type RepoSummary []*Summary
func NewRepoSummary(repo RepositoryName) RepoSummary {
return []*Summary{
{Repo: repo, Type: SummaryTypeImage},
{Repo: repo, Type: SummaryTypeImageIndex},
{Repo: repo, Type: SummaryTypeSociIndex},
}
}
func (s RepoSummary) toIndex(img ecrTypes.ImageDetail) int {
if isContainerImage(img) {
return 0
} else if isImageIndex(img) {
return 1
} else if isSociIndex(img) {
return 2
}
log.Printf("[warn] unknown image type: artifact:%s manifest:%s digest:%s",
aws.ToString(img.ArtifactMediaType),
aws.ToString(img.ImageManifestMediaType),
aws.ToString(img.ImageDigest),
)
return -1
}
func (s RepoSummary) Add(img ecrTypes.ImageDetail) {
index := s.toIndex(img)
if index >= 0 {
s[index].TotalImages++
s[index].TotalImageSize += aws.ToInt64(img.ImageSizeInBytes)
}
}
func (s RepoSummary) Expire(img ecrTypes.ImageDetail) {
index := s.toIndex(img)
if index >= 0 {
s[index].ExpiredImages++
s[index].ExpiredImageSize += aws.ToInt64(img.ImageSizeInBytes)
}
}
type Summary struct {
Repo RepositoryName `json:"repository"`
Type string `json:"type"`
ExpiredImages int64 `json:"expired_images"`
TotalImages int64 `json:"total_images"`
ExpiredImageSize int64 `json:"expired_image_size"`
TotalImageSize int64 `json:"total_image_size"`
}
func (s *Summary) printable() bool {
if s.Type == SummaryTypeImageIndex || s.Type == SummaryTypeSociIndex {
return s.TotalImages > 0
}
return true
}
func (s *Summary) row() []string {
return []string{
string(s.Repo),
s.Type,
fmt.Sprintf("%d (%s)", s.TotalImages, humanize.Bytes(uint64(s.TotalImageSize))),
fmt.Sprintf("%d (%s)", -s.ExpiredImages, humanize.Bytes(uint64(s.ExpiredImageSize))),
fmt.Sprintf("%d (%s)", s.TotalImages-s.ExpiredImages, humanize.Bytes(uint64(s.TotalImageSize-s.ExpiredImageSize))),
}
}
func newOutputFormatFrom(s string) outputFormat {
switch s {
case "table":
return formatTable
case "json":
return formatJSON
default:
panic(fmt.Sprintf("invalid format name: %s", s))
}
}
type outputFormat int
func (f outputFormat) String() string {
switch f {
case formatTable:
return "table"
case formatJSON:
return "json"
default:
return "unknown"
}
}
const (
formatTable outputFormat = iota + 1
formatJSON
)
type SummaryTable []*Summary
func (s SummaryTable) Sort() {
sort.SliceStable(s, func(i, j int) bool {
return s[i].Repo < s[j].Repo
})
}
func (s *SummaryTable) Print(w io.Writer, format outputFormat) error {
switch format {
case formatTable:
return s.printTable(w)
case formatJSON:
return s.printJSON(w)
default:
return fmt.Errorf("unknown output format: %s", format)
}
}
func (s SummaryTable) printJSON(w io.Writer) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
ss := lo.Filter(s, func(_s *Summary, _ int) bool {
return _s.printable()
})
return enc.Encode(ss)
}
func (s SummaryTable) printTable(w io.Writer) error {
t := tablewriter.NewWriter(w)
t.SetHeader(s.header())
t.SetBorder(false)
for _, s := range s {
row := s.row()
if !s.printable() {
continue
}
colors := make([]tablewriter.Colors, len(row))
if strings.HasPrefix(row[3], "0 ") {
row[3] = ""
} else {
colors[3] = tablewriter.Colors{tablewriter.FgBlueColor}
}
if strings.HasPrefix(row[4], "0 ") {
colors[4] = tablewriter.Colors{tablewriter.FgYellowColor}
}
if color.NoColor {
t.Append(row)
} else {
t.Rich(row, colors)
}
}
t.Render()
return nil
}
func (s SummaryTable) header() []string {
return []string{
"repository",
"type",
"total",
"expired",
"keep",
}
}