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

Move table columns so that the important column is displayed first #87

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Changes from all commits
Commits
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
44 changes: 27 additions & 17 deletions internal/output/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ import (
func PrintTableResults(vulnResult *models.VulnerabilityResults, outputWriter io.Writer) {
outputTable := table.NewWriter()
outputTable.SetOutputMirror(outputWriter)
outputTable.AppendHeader(table.Row{"Source", "Ecosystem", "Affected Package", "Version", "OSV URL (ID In Bold)"})
outputTable.AppendHeader(table.Row{"OSV URL (ID In Bold)", "Ecosystem", "Affected Package", "Version", "Source"})

width, _, err := term.GetSize(int(os.Stdout.Fd()))
isTerminal := false
if err == nil { // If output is a terminal, set max length to width and add styling
outputTable.SetStyle(table.StyleRounded)
outputTable.Style().Color.Row = text.Colors{text.Reset, text.BgHiBlack}
outputTable.Style().Color.RowAlternate = text.Colors{text.Reset, text.BgBlack}
outputTable.SetAllowedRowLength(width)
isTerminal = true
} // Otherwise use default ascii (e.g. getting piped to a file)

for _, sourceRes := range vulnResult.Results {
for _, pkg := range sourceRes.Packages {
workingDir, err := os.Getwd()
Expand All @@ -31,37 +42,36 @@ func PrintTableResults(vulnResult *models.VulnerabilityResults, outputWriter io.
}

for _, group := range pkg.Groups {
outputRow := table.Row{source.Path}
outputRow := table.Row{}
shouldMerge := false
if pkg.Package.Ecosystem == "GIT" {
outputRow = append(outputRow, "GIT", pkg.Package.Version, pkg.Package.Version)
shouldMerge = true
} else {
outputRow = append(outputRow, pkg.Package.Ecosystem, pkg.Package.Name, pkg.Package.Version)
}

var ids []string
var links []string

for _, vuln := range group.IDs {
ids = append(ids, vuln)
links = append(links, osv.BaseVulnerabilityURL+text.Bold.EscapeSeq()+vuln+text.Reset.EscapeSeq())
if isTerminal {
links = append(links, osv.BaseVulnerabilityURL+text.Bold.EscapeSeq()+vuln+text.Reset.EscapeSeq())
} else {
links = append(links, osv.BaseVulnerabilityURL+vuln)
}
}

outputRow = append(outputRow, strings.Join(links, "\n"))

if pkg.Package.Ecosystem == "GIT" {
outputRow = append(outputRow, "GIT", pkg.Package.Version, pkg.Package.Version)
shouldMerge = true
} else {
outputRow = append(outputRow, pkg.Package.Ecosystem, pkg.Package.Name, pkg.Package.Version)
}

outputRow = append(outputRow, source.Path)
outputTable.AppendRow(outputRow, table.RowConfig{AutoMerge: shouldMerge})
}
}
}

outputTable.SetStyle(table.StyleRounded)
outputTable.Style().Color.Row = text.Colors{text.Reset, text.BgHiBlack}
outputTable.Style().Color.RowAlternate = text.Colors{text.Reset, text.BgBlack}

width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err == nil { // If output is a terminal, set max length to width
outputTable.SetAllowedRowLength(width)
} // Otherwise don't set max width (e.g. getting piped to a file)
if outputTable.Length() == 0 {
return
}
Expand Down