@@ -39,6 +39,24 @@ type Warning struct {
39
39
Location pb.Location `json:"location,omitempty"`
40
40
}
41
41
42
+ func (w * Warning ) PrintTo (wr io.Writer , sources []* pb.SourceInfo ) error {
43
+ fmt .Fprintf (wr , "\n WARNING: %s" , w .RuleName )
44
+ if w .URL != "" {
45
+ fmt .Fprintf (wr , " - %s" , w .URL )
46
+ }
47
+ fmt .Fprintf (wr , "\n %s\n " , w .Detail )
48
+
49
+ if w .Location .SourceIndex < 0 {
50
+ return nil
51
+ }
52
+ sourceInfo := sources [w .Location .SourceIndex ]
53
+ source := errdefs.Source {
54
+ Info : sourceInfo ,
55
+ Ranges : w .Location .Ranges ,
56
+ }
57
+ return source .Print (wr )
58
+ }
59
+
42
60
type BuildError struct {
43
61
Message string `json:"message"`
44
62
Location pb.Location `json:"location"`
@@ -117,28 +135,7 @@ func (results *LintResults) ToResult() (*client.Result, error) {
117
135
return res , nil
118
136
}
119
137
120
- func (results * LintResults ) validateWarnings () error {
121
- for _ , warning := range results .Warnings {
122
- if int (warning .Location .SourceIndex ) >= len (results .Sources ) {
123
- return errors .Errorf ("sourceIndex is out of range" )
124
- }
125
- if warning .Location .SourceIndex > 0 {
126
- warningSource := results .Sources [warning .Location .SourceIndex ]
127
- if warningSource == nil {
128
- return errors .Errorf ("sourceIndex points to nil source" )
129
- }
130
- }
131
- }
132
- return nil
133
- }
134
-
135
- func PrintLintViolations (dt []byte , w io.Writer ) error {
136
- var results LintResults
137
-
138
- if err := json .Unmarshal (dt , & results ); err != nil {
139
- return err
140
- }
141
-
138
+ func (results * LintResults ) PrintTo (w io.Writer ) error {
142
139
if err := results .validateWarnings (); err != nil {
143
140
return err
144
141
}
@@ -169,21 +166,7 @@ func PrintLintViolations(dt []byte, w io.Writer) error {
169
166
})
170
167
171
168
for _ , warning := range results .Warnings {
172
- fmt .Fprintf (w , "\n WARNING: %s" , warning .RuleName )
173
- if warning .URL != "" {
174
- fmt .Fprintf (w , " - %s" , warning .URL )
175
- }
176
- fmt .Fprintf (w , "\n %s\n " , warning .Detail )
177
-
178
- if warning .Location .SourceIndex < 0 {
179
- continue
180
- }
181
- sourceInfo := results .Sources [warning .Location .SourceIndex ]
182
- source := errdefs.Source {
183
- Info : sourceInfo ,
184
- Ranges : warning .Location .Ranges ,
185
- }
186
- err := source .Print (w )
169
+ err := warning .PrintTo (w , results .Sources )
187
170
if err != nil {
188
171
return err
189
172
}
@@ -192,6 +175,47 @@ func PrintLintViolations(dt []byte, w io.Writer) error {
192
175
return nil
193
176
}
194
177
178
+ func (results * LintResults ) PrintErrorTo (w io.Writer ) {
179
+ // This prints out the error in LintResults to the writer in a format that
180
+ // is consistent with the warnings for easier downstream consumption.
181
+ if results .Error == nil {
182
+ return
183
+ }
184
+
185
+ fmt .Fprintln (w , results .Error .Message )
186
+ sourceInfo := results .Sources [results .Error .Location .SourceIndex ]
187
+ source := errdefs.Source {
188
+ Info : sourceInfo ,
189
+ Ranges : results .Error .Location .Ranges ,
190
+ }
191
+ source .Print (w )
192
+ }
193
+
194
+ func (results * LintResults ) validateWarnings () error {
195
+ for _ , warning := range results .Warnings {
196
+ if int (warning .Location .SourceIndex ) >= len (results .Sources ) {
197
+ return errors .Errorf ("sourceIndex is out of range" )
198
+ }
199
+ if warning .Location .SourceIndex > 0 {
200
+ warningSource := results .Sources [warning .Location .SourceIndex ]
201
+ if warningSource == nil {
202
+ return errors .Errorf ("sourceIndex points to nil source" )
203
+ }
204
+ }
205
+ }
206
+ return nil
207
+ }
208
+
209
+ func PrintLintViolations (dt []byte , w io.Writer ) error {
210
+ var results LintResults
211
+
212
+ if err := json .Unmarshal (dt , & results ); err != nil {
213
+ return err
214
+ }
215
+
216
+ return results .PrintTo (w )
217
+ }
218
+
195
219
func sourceInfoEqual (a , b * pb.SourceInfo ) bool {
196
220
if a .Filename != b .Filename || a .Language != b .Language {
197
221
return false
0 commit comments