Skip to content

Commit a61dd82

Browse files
committed
Add table output to results
1 parent f97e211 commit a61dd82

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

cmd/run.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
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 {

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ require (
1717
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
1818
github.com/hashicorp/hcl v1.0.0 // indirect
1919
github.com/inconshreveable/mousetrap v1.1.0 // indirect
20+
github.com/jedib0t/go-pretty/v6 v6.5.8 // indirect
2021
github.com/magiconair/properties v1.8.7 // indirect
22+
github.com/mattn/go-runewidth v0.0.15 // indirect
2123
github.com/mitchellh/mapstructure v1.5.0 // indirect
2224
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
25+
github.com/rivo/uniseg v0.2.0 // indirect
2326
github.com/sagikazarmark/locafero v0.4.0 // indirect
2427
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
2528
github.com/sourcegraph/conc v0.3.0 // indirect
@@ -30,7 +33,7 @@ require (
3033
go.uber.org/atomic v1.9.0 // indirect
3134
go.uber.org/multierr v1.9.0 // indirect
3235
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
33-
golang.org/x/sys v0.15.0 // indirect
36+
golang.org/x/sys v0.17.0 // indirect
3437
golang.org/x/text v0.14.0 // indirect
3538
gopkg.in/ini.v1 v1.67.0 // indirect
3639
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
3030
github.com/ianlancetaylor/demangle v0.0.0-20220319035150-800ac71e25c2/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
3131
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
3232
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
33+
github.com/jedib0t/go-pretty/v6 v6.5.8 h1:8BCzJdSvUbaDuRba4YVh+SKMGcAAKdkcF3SVFbrHAtQ=
34+
github.com/jedib0t/go-pretty/v6 v6.5.8/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
3335
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
3436
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
3537
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
@@ -41,13 +43,17 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4143
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
4244
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
4345
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
46+
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
47+
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
4448
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
4549
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
4650
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
4751
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
4852
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4953
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
5054
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
56+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
5157
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
5258
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
5359
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
@@ -103,6 +109,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
103109
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
104110
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
105111
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
112+
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
113+
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
106114
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
107115
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
108116
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)