99 "os"
1010 "path/filepath"
1111
12+ "github.com/jedib0t/go-pretty/v6/table"
1213 "github.com/spf13/cobra"
1314 "github.com/spf13/viper"
1415 pl "github.com/zph/polylint/pkg"
@@ -30,6 +31,7 @@ func Run(cmd *cobra.Command, args []string) (int, []error) {
3031 var exitCode int
3132 exitCode = 0
3233 var errs []error
34+ var results []pl.FileReport
3335 for _ , root := range args {
3436 configRaw , err := os .ReadFile (viper .ConfigFileUsed ())
3537
@@ -66,19 +68,48 @@ func Run(cmd *cobra.Command, args []string) (int, []error) {
6668 return fmt .Errorf ("error processing file %q: %v" , path , err2 )
6769 }
6870
69- if len (result .Findings ) > 0 {
70- fmt .Printf ("\n %s: violations count %d\n " , result .Path , len (result .Findings ))
71- for idx , finding := range result .Findings {
72- fmt .Printf ("%d: Line %3d %30s %20s\n " , idx + 1 , finding .LineNo , finding .RuleId , finding .Rule .Description )
73- }
74- exitCode = 1
75- }
71+ results = append (results , result )
7672 }
7773
7874 return nil
7975 })
8076 errs = append (errs , err )
8177 }
78+
79+ t := table .NewWriter ()
80+ t .SetOutputMirror (os .Stdout )
81+ t .AppendHeader (table.Row {"File" , "#" , "Scope" , "Rule Id" , "Recommendation" , "Link" })
82+
83+ summary := table .NewWriter ()
84+ summary .SetOutputMirror (os .Stdout )
85+ summary .AppendHeader (table.Row {"File" , "Violations" })
86+ summary .SortBy ([]table.SortBy {
87+ {Name : "Violations" , Mode : table .Dsc },
88+ })
89+ for _ , result := range results {
90+ if len (result .Findings ) > 0 {
91+ summary .AppendRow ([]interface {}{result .Path , len (result .Findings )})
92+ for idx , finding := range result .Findings {
93+ var scope string
94+ if finding .Rule .Scope == "file" || finding .Rule .Scope == "path" {
95+ scope = fmt .Sprintf ("%s" , finding .Rule .Scope )
96+ } else {
97+ scope = fmt .Sprintf ("%s %3d" , finding .Rule .Scope , finding .LineNo )
98+ }
99+ t .AppendRow ([]interface {}{
100+ result .Path , idx + 1 , scope , finding .RuleId , finding .Rule .Recommendation , finding .Rule .Link ,
101+ })
102+ exitCode += 1
103+ }
104+ }
105+ }
106+ summary .Render ()
107+ t .Render ()
108+
109+ if exitCode > 255 {
110+ exitCode = 255
111+ }
112+
82113 nonNilErrors := make ([]error , 0 )
83114 for _ , e := range errs {
84115 if e != nil {
0 commit comments