1
1
package utils
2
2
3
- import (
4
- "sort"
5
-
6
- "code.cloudfoundry.org/bytefmt"
7
- )
8
-
9
- var SortSize bool
10
-
11
3
type Result interface {
12
- GetStruct () interface {}
4
+ OutputStruct () interface {}
13
5
OutputText (resultType string ) error
14
6
}
15
7
@@ -21,7 +13,7 @@ type AnalyzeResult struct {
21
13
22
14
type ListAnalyzeResult AnalyzeResult
23
15
24
- func (r ListAnalyzeResult ) GetStruct () interface {} {
16
+ func (r ListAnalyzeResult ) OutputStruct () interface {} {
25
17
return r
26
18
}
27
19
@@ -32,7 +24,7 @@ func (r ListAnalyzeResult) OutputText(resultType string) error {
32
24
33
25
type MultiVersionPackageAnalyzeResult AnalyzeResult
34
26
35
- func (r MultiVersionPackageAnalyzeResult ) GetStruct () interface {} {
27
+ func (r MultiVersionPackageAnalyzeResult ) OutputStruct () interface {} {
36
28
analysis := r .Analysis .(map [string ]map [string ]PackageInfo )
37
29
analysisOutput := getMultiVersionPackageOutput (analysis )
38
30
output := struct {
@@ -66,7 +58,7 @@ func (r MultiVersionPackageAnalyzeResult) OutputText(resultType string) error {
66
58
67
59
type SingleVersionPackageAnalyzeResult AnalyzeResult
68
60
69
- func (r SingleVersionPackageAnalyzeResult ) GetStruct () interface {} {
61
+ func (r SingleVersionPackageAnalyzeResult ) OutputStruct () interface {} {
70
62
analysis := r .Analysis .(map [string ]PackageInfo )
71
63
analysisOutput := getSingleVersionPackageOutput (analysis )
72
64
output := struct {
@@ -105,56 +97,6 @@ type PackageOutput struct {
105
97
Size int64
106
98
}
107
99
108
- type packageBy func (p1 , p2 * PackageOutput ) bool
109
-
110
- func (by packageBy ) Sort (packages []PackageOutput ) {
111
- ps := & packageSorter {
112
- packages : packages ,
113
- by : by ,
114
- }
115
- sort .Sort (ps )
116
- }
117
-
118
- type packageSorter struct {
119
- packages []PackageOutput
120
- by func (p1 , p2 * PackageOutput ) bool
121
- }
122
-
123
- func (s * packageSorter ) Len () int {
124
- return len (s .packages )
125
- }
126
-
127
- func (s * packageSorter ) Less (i , j int ) bool {
128
- return s .by (& s .packages [i ], & s .packages [j ])
129
- }
130
-
131
- func (s * packageSorter ) Swap (i , j int ) {
132
- s .packages [i ], s .packages [j ] = s .packages [j ], s .packages [i ]
133
- }
134
-
135
- // If packages have the same name, means they exist where multiple version of the same package are allowed,
136
- // so sort by version. If they have the same version, then sort by size.
137
- var packageNameSort = func (p1 , p2 * PackageOutput ) bool {
138
- if p1 .Name == p2 .Name {
139
- if p1 .Version == p2 .Version {
140
- return p1 .Size > p2 .Size
141
- }
142
- return p1 .Version < p2 .Version
143
- }
144
- return p1 .Name < p2 .Name
145
- }
146
-
147
- // If packages have the same size, sort by name. If they are two versions of the same package, sort by version.
148
- var packageSizeSort = func (p1 , p2 * PackageOutput ) bool {
149
- if p1 .Size == p2 .Size {
150
- if p1 .Name == p2 .Name {
151
- return p1 .Version < p2 .Version
152
- }
153
- return p1 .Name < p2 .Name
154
- }
155
- return p1 .Size > p2 .Size
156
- }
157
-
158
100
func getSingleVersionPackageOutput (packageMap map [string ]PackageInfo ) []PackageOutput {
159
101
packages := []PackageOutput {}
160
102
for name , info := range packageMap {
@@ -185,33 +127,9 @@ func getMultiVersionPackageOutput(packageMap map[string]map[string]PackageInfo)
185
127
return packages
186
128
}
187
129
188
- type StrPackageOutput struct {
189
- Name string
190
- Path string
191
- Version string
192
- Size string
193
- }
194
-
195
- func stringifySize (size int64 ) string {
196
- strSize := "unknown"
197
- if size != - 1 {
198
- strSize = bytefmt .ByteSize (uint64 (size ))
199
- }
200
- return strSize
201
- }
202
-
203
- func stringifyPackages (packages []PackageOutput ) []StrPackageOutput {
204
- strPackages := []StrPackageOutput {}
205
- for _ , pack := range packages {
206
- strSize := stringifySize (pack .Size )
207
- strPackages = append (strPackages , StrPackageOutput {pack .Name , pack .Path , pack .Version , strSize })
208
- }
209
- return strPackages
210
- }
211
-
212
130
type FileAnalyzeResult AnalyzeResult
213
131
214
- func (r FileAnalyzeResult ) GetStruct () interface {} {
132
+ func (r FileAnalyzeResult ) OutputStruct () interface {} {
215
133
analysis := r .Analysis .([]DirectoryEntry )
216
134
if SortSize {
217
135
directoryBy (directorySizeSort ).Sort (analysis )
@@ -242,57 +160,3 @@ func (r FileAnalyzeResult) OutputText(analyzeType string) error {
242
160
}
243
161
return TemplateOutput (strResult , "FileAnalyze" )
244
162
}
245
-
246
- type directoryBy func (e1 , e2 * DirectoryEntry ) bool
247
-
248
- func (by directoryBy ) Sort (entries []DirectoryEntry ) {
249
- ds := & directorySorter {
250
- entries : entries ,
251
- by : by ,
252
- }
253
- sort .Sort (ds )
254
- }
255
-
256
- type directorySorter struct {
257
- entries []DirectoryEntry
258
- by func (p1 , p2 * DirectoryEntry ) bool
259
- }
260
-
261
- func (s * directorySorter ) Len () int {
262
- return len (s .entries )
263
- }
264
-
265
- func (s * directorySorter ) Less (i , j int ) bool {
266
- return s .by (& s .entries [i ], & s .entries [j ])
267
- }
268
-
269
- func (s * directorySorter ) Swap (i , j int ) {
270
- s .entries [i ], s .entries [j ] = s .entries [j ], s .entries [i ]
271
- }
272
-
273
- // If packages have the same name, means they exist where multiple version of the same package are allowed,
274
- // so sort by version. If they have the same version, then sort by size.
275
- var directoryNameSort = func (e1 , e2 * DirectoryEntry ) bool {
276
- return e1 .Name < e2 .Name
277
- }
278
-
279
- // If packages have the same size, sort by name. If they are two versions of the same package, sort by version.
280
- var directorySizeSort = func (e1 , e2 * DirectoryEntry ) bool {
281
- if e1 .Size == e2 .Size {
282
- return e1 .Name < e2 .Name
283
- }
284
- return e1 .Size > e2 .Size
285
- }
286
-
287
- type StrDirectoryEntry struct {
288
- Name string
289
- Size string
290
- }
291
-
292
- func stringifyDirectoryEntries (entries []DirectoryEntry ) (strEntries []StrDirectoryEntry ) {
293
- for _ , entry := range entries {
294
- strEntry := StrDirectoryEntry {Name : entry .Name , Size : stringifySize (entry .Size )}
295
- strEntries = append (strEntries , strEntry )
296
- }
297
- return
298
- }
0 commit comments