1
1
package utils
2
2
3
+ import (
4
+ "errors"
5
+ "fmt"
6
+
7
+ "github.com/golang/glog"
8
+ )
9
+
3
10
type Result interface {
4
11
OutputStruct () interface {}
5
12
OutputText (resultType string ) error
@@ -18,14 +25,24 @@ func (r ListAnalyzeResult) OutputStruct() interface{} {
18
25
}
19
26
20
27
func (r ListAnalyzeResult ) OutputText (resultType string ) error {
21
- r .Analysis = r .Analysis .([]string )
28
+ analysis , valid := r .Analysis .([]string )
29
+ if ! valid {
30
+ glog .Error ("Unexpected structure of Analysis. Should be of type []string" )
31
+ return errors .New (fmt .Sprintf ("Could not output %s analysis result" , r .AnalyzeType ))
32
+ }
33
+ r .Analysis = analysis
34
+
22
35
return TemplateOutput (r , "ListAnalyze" )
23
36
}
24
37
25
38
type MultiVersionPackageAnalyzeResult AnalyzeResult
26
39
27
40
func (r MultiVersionPackageAnalyzeResult ) OutputStruct () interface {} {
28
- analysis := r .Analysis .(map [string ]map [string ]PackageInfo )
41
+ analysis , valid := r .Analysis .(map [string ]map [string ]PackageInfo )
42
+ if ! valid {
43
+ glog .Error ("Unexpected structure of Analysis. Should be of type map[string]map[string]PackageInfo" )
44
+ return errors .New (fmt .Sprintf ("Could not output %s analysis result" , r .AnalyzeType ))
45
+ }
29
46
analysisOutput := getMultiVersionPackageOutput (analysis )
30
47
output := struct {
31
48
Image string
@@ -40,7 +57,11 @@ func (r MultiVersionPackageAnalyzeResult) OutputStruct() interface{} {
40
57
}
41
58
42
59
func (r MultiVersionPackageAnalyzeResult ) OutputText (resultType string ) error {
43
- analysis := r .Analysis .(map [string ]map [string ]PackageInfo )
60
+ analysis , valid := r .Analysis .(map [string ]map [string ]PackageInfo )
61
+ if ! valid {
62
+ glog .Error ("Unexpected structure of Analysis. Should be of type map[string]map[string]PackageInfo" )
63
+ return errors .New (fmt .Sprintf ("Could not output %s analysis result" , r .AnalyzeType ))
64
+ }
44
65
analysisOutput := getMultiVersionPackageOutput (analysis )
45
66
46
67
strAnalysis := stringifyPackages (analysisOutput )
@@ -59,7 +80,11 @@ func (r MultiVersionPackageAnalyzeResult) OutputText(resultType string) error {
59
80
type SingleVersionPackageAnalyzeResult AnalyzeResult
60
81
61
82
func (r SingleVersionPackageAnalyzeResult ) OutputStruct () interface {} {
62
- analysis := r .Analysis .(map [string ]PackageInfo )
83
+ analysis , valid := r .Analysis .(map [string ]PackageInfo )
84
+ if ! valid {
85
+ glog .Error ("Unexpected structure of Analysis. Should be of type map[string]PackageInfo" )
86
+ return errors .New (fmt .Sprintf ("Could not output %s analysis result" , r .AnalyzeType ))
87
+ }
63
88
analysisOutput := getSingleVersionPackageOutput (analysis )
64
89
output := struct {
65
90
Image string
@@ -74,7 +99,11 @@ func (r SingleVersionPackageAnalyzeResult) OutputStruct() interface{} {
74
99
}
75
100
76
101
func (r SingleVersionPackageAnalyzeResult ) OutputText (diffType string ) error {
77
- analysis := r .Analysis .(map [string ]PackageInfo )
102
+ analysis , valid := r .Analysis .(map [string ]PackageInfo )
103
+ if ! valid {
104
+ glog .Error ("Unexpected structure of Analysis. Should be of type map[string]PackageInfo" )
105
+ return errors .New (fmt .Sprintf ("Could not output %s analysis result" , r .AnalyzeType ))
106
+ }
78
107
analysisOutput := getSingleVersionPackageOutput (analysis )
79
108
80
109
strAnalysis := stringifyPackages (analysisOutput )
@@ -130,7 +159,12 @@ func getMultiVersionPackageOutput(packageMap map[string]map[string]PackageInfo)
130
159
type FileAnalyzeResult AnalyzeResult
131
160
132
161
func (r FileAnalyzeResult ) OutputStruct () interface {} {
133
- analysis := r .Analysis .([]DirectoryEntry )
162
+ analysis , valid := r .Analysis .([]DirectoryEntry )
163
+ if ! valid {
164
+ glog .Error ("Unexpected structure of Analysis. Should be of type []DirectoryEntry" )
165
+ return errors .New ("Could not output FileAnalyzer analysis result" )
166
+ }
167
+
134
168
if SortSize {
135
169
directoryBy (directorySizeSort ).Sort (analysis )
136
170
} else {
@@ -141,7 +175,12 @@ func (r FileAnalyzeResult) OutputStruct() interface{} {
141
175
}
142
176
143
177
func (r FileAnalyzeResult ) OutputText (analyzeType string ) error {
144
- analysis := r .Analysis .([]DirectoryEntry )
178
+ analysis , valid := r .Analysis .([]DirectoryEntry )
179
+ if ! valid {
180
+ glog .Error ("Unexpected structure of Analysis. Should be of type []DirectoryEntry" )
181
+ return errors .New ("Could not output FileAnalyzer analysis result" )
182
+ }
183
+
145
184
if SortSize {
146
185
directoryBy (directorySizeSort ).Sort (analysis )
147
186
} else {
0 commit comments