Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create options for human-readable output formats #437

Merged
merged 27 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3e7fe69
Add a -summary option to print a short summary of the linting
acaird May 25, 2020
dbb3332
Added -longsummary option and output
acaird May 25, 2020
eef788b
spelling fix
zakird May 25, 2020
31c7bdc
Remove tablewriter dependency and reimplement the good parts
acaird May 28, 2020
eefe940
spelling fix
zakird May 25, 2020
ec56f06
Merge branch 'summary' of github.com:acaird/zlint into summary
acaird May 28, 2020
b285882
Fixed a missed merge :(
acaird May 28, 2020
fb86585
Merge branch 'master' into summary
cpu May 31, 2020
832bc81
switched longsummary to longSummary; fixed output bug
acaird Jun 2, 2020
b3c3f69
Cleaned up typos, variable names, formatting
acaird Jun 2, 2020
3a60980
Merge branch 'summary' of github.com:acaird/zlint into summary
acaird Jun 3, 2020
aab1521
Merge branch 'master' into summary
cpu Jun 7, 2020
1e6d785
Merge branch 'master' into summary
cpu Jun 11, 2020
4be536f
Merge branch 'master' into summary
cpu Jun 25, 2020
796d10f
parent 99579098a16c10d1d704b8b8149dd8c35329107f
acaird May 25, 2020
83d5bfe
autopull: 2020-05-27T14:34:02Z (#441)
tld-update-bot May 27, 2020
307f3e8
gTLD autopull: 2020-05-28T14:35:00Z (#442)
tld-update-bot May 28, 2020
3aaea14
Moved structure creation out of function into a method for reporting
acaird Jun 28, 2020
e67c57f
Merge branch 'summary' of github.com:acaird/zlint into summary
acaird Jun 28, 2020
0a21f09
Merge branch 'master' into summary
cpu Jul 2, 2020
e12bda4
Moved the formatted output routines out of main
acaird Jul 5, 2020
28441a5
Merge branch 'summary' of github.com:acaird/zlint into summary
acaird Jul 5, 2020
d2079c5
Changed newRT to a pointer receiver
acaird Jul 8, 2020
9798f3f
Changed output options to all them all; newlines for nice output
acaird Jul 11, 2020
c21f46c
Changed output options to allow printing of them all; newlines for ni…
acaird Jul 11, 2020
8a79935
Merge branch 'summary' of github.com:acaird/zlint into summary
acaird Jul 11, 2020
d436af9
Merge branch 'master' into summary
cpu Jul 20, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 91 additions & 1 deletion v2/cmd/zlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (
"os"
"regexp"
"sort"
"strconv"
"strings"

"github.com/olekukonko/tablewriter"
log "github.com/sirupsen/logrus"
"github.com/zmap/zcrypto/x509"
"github.com/zmap/zlint/v2"
Expand All @@ -36,6 +38,8 @@ import (
var ( // flags
listLintsJSON bool
listLintSources bool
summary bool
longsummary bool
prettyprint bool
format string
nameFilter string
Expand All @@ -51,14 +55,16 @@ var ( // flags
func init() {
flag.BoolVar(&listLintsJSON, "list-lints-json", false, "Print lints in JSON format, one per line")
flag.BoolVar(&listLintSources, "list-lints-source", false, "Print list of lint sources, one per line")
flag.BoolVar(&summary, "summary", false, "Prints a short human-readable summary report")
flag.BoolVar(&longsummary, "longsummary", false, "Prints a human-readable summary report with details")
acaird marked this conversation as resolved.
Show resolved Hide resolved
flag.StringVar(&format, "format", "pem", "One of {pem, der, base64}")
flag.StringVar(&nameFilter, "nameFilter", "", "Only run lints with a name matching the provided regex. (Can not be used with -includeNames/-excludeNames)")
flag.StringVar(&includeNames, "includeNames", "", "Comma-separated list of lints to include by name")
flag.StringVar(&excludeNames, "excludeNames", "", "Comma-separated list of lints to exclude by name")
flag.StringVar(&includeSources, "includeSources", "", "Comma-separated list of lint sources to include")
flag.StringVar(&excludeSources, "excludeSources", "", "Comma-separated list of lint sources to exclude")

flag.BoolVar(&prettyprint, "pretty", false, "Pretty-print output")
flag.BoolVar(&prettyprint, "pretty", false, "Pretty-print JSON output")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "ZLint version %s\n\n", version)
fmt.Fprintf(os.Stderr, "Usage: %s [flags] file...\n", os.Args[0])
Expand Down Expand Up @@ -156,6 +162,10 @@ func doLint(inputFile *os.File, inform string, registry lint.Registry) {
log.Fatalf("can't format output: %s", err)
}
os.Stdout.Write(out.Bytes())
} else if summary {
outputSummary(zlintResult, false)
} else if longsummary {
outputSummary(zlintResult, true)
} else {
os.Stdout.Write(jsonBytes)
}
Expand Down Expand Up @@ -210,3 +220,83 @@ func setLints() (lint.Registry, error) {

return lint.GlobalRegistry().Filter(filterOpts)
}

func outputSummary(zlintResult *zlint.ResultSet, longSummary bool) {
cpu marked this conversation as resolved.
Show resolved Hide resolved
var sortedLevels []int
resultCount := make(map[lint.LintStatus]int)
resultDetails := make(map[lint.LintStatus][]string)
lintLevelsAboveThreshold := make(map[int]lint.LintStatus)
// Set the threashold under which (inclusive) events are not
// counted
threshold := lint.Pass

// Make the list of lint levels that matter
for _, i := range lint.StatusLabelToLintStatus {
if i <= threshold {
continue
}
lintLevelsAboveThreshold[int(i)] = i
}
// Set all of the levels to 0 events to they are all displayed
acaird marked this conversation as resolved.
Show resolved Hide resolved
// in the -summary table
for _, level := range lintLevelsAboveThreshold {
resultCount[level] = 0
}
// Count up the number of each event
for key, value := range zlintResult.Results {
acaird marked this conversation as resolved.
Show resolved Hide resolved
if value.Status > threshold {
resultCount[value.Status]++
if longSummary {
resultDetails[value.Status] = append(
resultDetails[value.Status],
string(key),
)
}
}
}
// Sort the levels we have so we can get a nice output
for key, _ := range resultCount {
sortedLevels = append(sortedLevels, int(key))
}
sort.Ints(sortedLevels)
// make the requested table type
if longSummary {
// make a table with the internal lint names grouped
// by type
longTable := tablewriter.NewWriter(os.Stdout)
longTable.SetHeader([]string{"Level", "# occurances", "Details"})
longTable.SetAutoMergeCells(true)
for _, level := range sortedLevels {
foundDetail := false
for _, detail := range resultDetails[lint.LintStatus(level)] {
longTable.Append([]string{
fmt.Sprintf("%s", lint.LintStatus(level)),
strconv.Itoa(resultCount[lint.LintStatus(level)]),
detail,
})
foundDetail = true
}
if !foundDetail {
longTable.Append([]string{
fmt.Sprintf("%s", lint.LintStatus(level)),
strconv.Itoa(resultCount[lint.LintStatus(level)]),
" - ",
})
}
}
longTable.Render()
} else {
// Make a table of the count of each error type and
// print it nicely
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Level", "# occurances"})

for _, level := range sortedLevels {
table.Append([]string{
fmt.Sprintf("%s", lint.LintStatus(level)),
strconv.Itoa(resultCount[lint.LintStatus(level)]),
})
}
table.Render()
}
}
1 change: 1 addition & 0 deletions v2/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/zmap/zlint/v2

require (
github.com/olekukonko/tablewriter v0.0.4
cpu marked this conversation as resolved.
Show resolved Hide resolved
github.com/sirupsen/logrus v1.3.0
github.com/weppos/publicsuffix-go v0.4.0
github.com/zmap/zcrypto v0.0.0-20200513165325-16679db567ff
Expand Down
4 changes: 4 additions & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474/go.mod h1:OQA4XLvDbMgS8P0CevmM4m9Q3Jq4phKUzcocxuGJ5m8=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
6 changes: 3 additions & 3 deletions v2/lint/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ const (
)

var (
// statusLabelToLintStatus is used to work backwards from
// StatusLabelToLintStatus is used to work backwards from
// a LintStatus.String() to the LintStatus. This is used by
// LintStatus.Unmarshal.
statusLabelToLintStatus = map[string]LintStatus{
StatusLabelToLintStatus = map[string]LintStatus{
Reserved.String(): Reserved,
NA.String(): NA,
NE.String(): NE,
Expand Down Expand Up @@ -73,7 +73,7 @@ func (e LintStatus) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements the json.Unmarshaler interface.
func (e *LintStatus) UnmarshalJSON(data []byte) error {
key := strings.ReplaceAll(string(data), `"`, "")
if status, ok := statusLabelToLintStatus[key]; ok {
if status, ok := StatusLabelToLintStatus[key]; ok {
*e = status
} else {
return fmt.Errorf("bad LintStatus JSON value: %s", string(data))
Expand Down