Skip to content

Commit 8a0d47e

Browse files
committed
Refactor reporting function
1 parent d284853 commit 8a0d47e

File tree

2 files changed

+101
-72
lines changed

2 files changed

+101
-72
lines changed

cmd/diagnose.go

+5-72
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
package cmd
22

33
import (
4-
"errors"
54
"fmt"
65
"log"
76
"os"
87
"sort"
98
"strings"
109
"sync"
1110

12-
"github.com/MakeNowJust/heredoc"
1311
"github.com/aquasecurity/go-dep-parser/pkg/io"
1412
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
1513
"github.com/aquasecurity/go-dep-parser/pkg/types"
16-
"github.com/fatih/color"
1714
dart "github.com/kyoshidajp/dep-doctor/cmd/dart/pub"
1815
erlang_elixir "github.com/kyoshidajp/dep-doctor/cmd/erlang_elixir/hex"
1916
"github.com/kyoshidajp/dep-doctor/cmd/github"
@@ -181,6 +178,11 @@ func Diagnose(d Doctor, r io.ReadSeekCloserAt, year int, ignores []string) map[s
181178
return diagnoses
182179
}
183180

181+
func Report(diagnoses map[string]Diagnosis, strict_mode bool) error {
182+
reporter := NewStdoutReporter(diagnoses, strict_mode)
183+
return reporter.Report()
184+
}
185+
184186
type Options struct {
185187
packageManager string
186188
filePath string
@@ -270,72 +272,3 @@ func init() {
270272
fmt.Println(err.Error())
271273
}
272274
}
273-
274-
func Report(diagnoses map[string]Diagnosis, strict_mode bool) error {
275-
errMessages, warnMessages, ignoredMessages := []string{}, []string{}, []string{}
276-
errCount, warnCount, infoCount := 0, 0, 0
277-
unDiagnosedCount, ignoredCount := 0, 0
278-
279-
lib_names := make([]string, 0, len(diagnoses))
280-
for key := range diagnoses {
281-
lib_names = append(lib_names, key)
282-
}
283-
sort.Strings(lib_names)
284-
285-
for _, lib_name := range lib_names {
286-
diagnosis := diagnoses[lib_name]
287-
if diagnosis.Ignored {
288-
ignoredMessages = append(ignoredMessages, fmt.Sprintf("[info] %s (ignored):", diagnosis.Name))
289-
ignoredCount += 1
290-
infoCount += 1
291-
continue
292-
}
293-
294-
if diagnosis.Error != nil {
295-
errMessages = append(errMessages, fmt.Sprintf("[error] %s: %s", diagnosis.Name, diagnosis.Error))
296-
errCount += 1
297-
continue
298-
}
299-
300-
if !diagnosis.Diagnosed {
301-
warnMessages = append(warnMessages, fmt.Sprintf("[warn] %s (unknown): %s", diagnosis.Name, diagnosis.ErrorMessage()))
302-
unDiagnosedCount += 1
303-
warnCount += 1
304-
continue
305-
}
306-
if diagnosis.Archived {
307-
errMessages = append(errMessages, fmt.Sprintf("[error] %s (archived): %s", diagnosis.Name, diagnosis.URL))
308-
errCount += 1
309-
}
310-
if !diagnosis.IsActive {
311-
warnMessages = append(warnMessages, fmt.Sprintf("[warn] %s (not-maintained): %s", diagnosis.Name, diagnosis.URL))
312-
warnCount += 1
313-
}
314-
}
315-
316-
fmt.Printf("\n")
317-
if len(ignoredMessages) > 0 {
318-
fmt.Println(strings.Join(ignoredMessages, "\n"))
319-
}
320-
if len(warnMessages) > 0 {
321-
color.Yellow(strings.Join(warnMessages, "\n"))
322-
}
323-
if len(errMessages) > 0 {
324-
color.Red(strings.Join(errMessages, "\n"))
325-
}
326-
327-
color.Green(heredoc.Docf(`
328-
Diagnosis completed! %d libraries.
329-
%d error, %d warn (%d unknown), %d info (%d ignored)`,
330-
len(diagnoses),
331-
errCount,
332-
warnCount, unDiagnosedCount,
333-
infoCount, ignoredCount),
334-
)
335-
336-
if len(errMessages) > 0 || strict_mode && warnCount > 0 {
337-
return errors.New("has error")
338-
}
339-
340-
return nil
341-
}

cmd/report.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"sort"
7+
"strings"
8+
9+
"github.com/MakeNowJust/heredoc"
10+
"github.com/fatih/color"
11+
)
12+
13+
type Reporter interface {
14+
Report() error
15+
}
16+
17+
type StdoutReporter struct {
18+
diagnoses map[string]Diagnosis
19+
strict_mode bool
20+
}
21+
22+
func NewStdoutReporter(diagnosis map[string]Diagnosis, strict_mode bool) *StdoutReporter {
23+
return &StdoutReporter{
24+
diagnoses: diagnosis,
25+
strict_mode: strict_mode,
26+
}
27+
}
28+
29+
func (r *StdoutReporter) Report() error {
30+
errMessages, warnMessages, ignoredMessages := []string{}, []string{}, []string{}
31+
errCount, warnCount, infoCount := 0, 0, 0
32+
unDiagnosedCount, ignoredCount := 0, 0
33+
34+
lib_names := make([]string, 0, len(r.diagnoses))
35+
for key := range r.diagnoses {
36+
lib_names = append(lib_names, key)
37+
}
38+
sort.Strings(lib_names)
39+
40+
for _, lib_name := range lib_names {
41+
diagnosis := r.diagnoses[lib_name]
42+
if diagnosis.Ignored {
43+
ignoredMessages = append(ignoredMessages, fmt.Sprintf("[info] %s (ignored):", diagnosis.Name))
44+
ignoredCount += 1
45+
infoCount += 1
46+
continue
47+
}
48+
49+
if diagnosis.Error != nil {
50+
errMessages = append(errMessages, fmt.Sprintf("[error] %s: %s", diagnosis.Name, diagnosis.Error))
51+
errCount += 1
52+
continue
53+
}
54+
55+
if !diagnosis.Diagnosed {
56+
warnMessages = append(warnMessages, fmt.Sprintf("[warn] %s (unknown): %s", diagnosis.Name, diagnosis.ErrorMessage()))
57+
unDiagnosedCount += 1
58+
warnCount += 1
59+
continue
60+
}
61+
if diagnosis.Archived {
62+
errMessages = append(errMessages, fmt.Sprintf("[error] %s (archived): %s", diagnosis.Name, diagnosis.URL))
63+
errCount += 1
64+
}
65+
if !diagnosis.IsActive {
66+
warnMessages = append(warnMessages, fmt.Sprintf("[warn] %s (not-maintained): %s", diagnosis.Name, diagnosis.URL))
67+
warnCount += 1
68+
}
69+
}
70+
71+
fmt.Printf("\n")
72+
if ignoredCount > 0 {
73+
fmt.Println(strings.Join(ignoredMessages, "\n"))
74+
}
75+
if warnCount > 0 {
76+
color.Yellow(strings.Join(warnMessages, "\n"))
77+
}
78+
if errCount > 0 {
79+
color.Red(strings.Join(errMessages, "\n"))
80+
}
81+
82+
color.Green(heredoc.Docf(`
83+
Diagnosis completed! %d libraries.
84+
%d error, %d warn (%d unknown), %d info (%d ignored)`,
85+
len(r.diagnoses),
86+
errCount,
87+
warnCount, unDiagnosedCount,
88+
infoCount, ignoredCount),
89+
)
90+
91+
if errCount > 0 || r.strict_mode && warnCount > 0 {
92+
return errors.New("has error")
93+
}
94+
95+
return nil
96+
}

0 commit comments

Comments
 (0)