Skip to content

Commit c9a4c79

Browse files
Clean up output formatting across commands
- Rename "human" output format to "pretty" in issues, metrics, and vulnerabilities - Simplify issues table: remove boxed style, drop wrapText, conditionally show SOURCE column - Humanize displayed values (severity, status, reachability, ecosystem) - Add ECOSYSTEM column to vulnerabilities table - Use helper functions for formatting location, severity, and analyzer names
1 parent 11f0a59 commit c9a4c79

3 files changed

Lines changed: 86 additions & 111 deletions

File tree

command/issues/issues.go

Lines changed: 40 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type IssuesOptions struct {
4141
func NewCmdIssues() *cobra.Command {
4242
opts := IssuesOptions{
4343
LimitArg: 30,
44-
OutputFormat: "human",
44+
OutputFormat: "pretty",
4545
}
4646

4747
doc := heredoc.Docf(`
@@ -81,7 +81,7 @@ func NewCmdIssues() *cobra.Command {
8181
cmd.Flags().IntVarP(&opts.LimitArg, "limit", "l", 30, "Maximum number of issues to fetch")
8282

8383
// --output, -o flag
84-
cmd.Flags().StringVarP(&opts.OutputFormat, "output", "o", "human", "Output format: human, table, json, yaml")
84+
cmd.Flags().StringVarP(&opts.OutputFormat, "output", "o", "pretty", "Output format: pretty, table, json, yaml")
8585

8686
// --output-file flag
8787
cmd.Flags().StringVar(&opts.OutputFile, "output-file", "", "Write output to a file instead of stdout")
@@ -107,7 +107,7 @@ func NewCmdIssues() *cobra.Command {
107107
})
108108
_ = cmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
109109
return []string{
110-
"human\tHuman-readable grouped output",
110+
"pretty\tPretty-printed grouped output",
111111
"table\tTable output",
112112
"json\tJSON output",
113113
"yaml\tYAML output",
@@ -360,54 +360,48 @@ func (opts *IssuesOptions) outputTable() error {
360360
return nil
361361
}
362362

363-
header := []string{"LOCATION", "SOURCE", "ANALYZER", "CODE", "TITLE", "CATEGORY", "SEVERITY"}
364-
data := [][]string{header}
365-
366-
terminalWidth := pterm.GetTerminalWidth()
367-
if terminalWidth == 0 {
368-
terminalWidth = 120
369-
}
363+
showSource := opts.CommitOid != "" || opts.PRNumber > 0
370364

371-
colWidths := []int{
372-
int(float64(terminalWidth) * 0.18), // LOCATION
373-
int(float64(terminalWidth) * 0.08), // SOURCE
374-
int(float64(terminalWidth) * 0.10), // ANALYZER
375-
int(float64(terminalWidth) * 0.10), // CODE
376-
int(float64(terminalWidth) * 0.30), // TITLE
377-
int(float64(terminalWidth) * 0.14), // CATEGORY
378-
int(float64(terminalWidth) * 0.10), // SEVERITY
365+
var header []string
366+
if showSource {
367+
header = []string{"LOCATION", "SOURCE", "ANALYZER", "CODE", "TITLE", "CATEGORY", "SEVERITY"}
368+
} else {
369+
header = []string{"LOCATION", "ANALYZER", "CODE", "TITLE", "CATEGORY", "SEVERITY"}
379370
}
371+
data := [][]string{header}
380372

381373
cwd, _ := os.Getwd()
382374

383375
for _, issue := range opts.issues {
384-
filePath := issue.Location.Path
385-
if cwd != "" && strings.HasPrefix(filePath, cwd) {
386-
filePath = strings.TrimPrefix(filePath, cwd+"/")
387-
}
388-
389-
var issueLocation string
390-
if issue.Location.Position.BeginLine == issue.Location.Position.EndLine {
391-
issueLocation = fmt.Sprintf("%s:%d", filePath, issue.Location.Position.BeginLine)
376+
location := formatLocation(issue, cwd)
377+
severity := formatSeverity(issue.IssueSeverity)
378+
category := humanizeCategory(issue.IssueCategory)
379+
analyzer := analyzerDisplayName(issue.Analyzer)
380+
381+
if showSource {
382+
data = append(data, []string{
383+
location,
384+
issue.IssueSource,
385+
analyzer,
386+
issue.IssueCode,
387+
issue.IssueText,
388+
category,
389+
severity,
390+
})
392391
} else {
393-
issueLocation = fmt.Sprintf("%s:%d-%d", filePath, issue.Location.Position.BeginLine, issue.Location.Position.EndLine)
392+
data = append(data, []string{
393+
location,
394+
analyzer,
395+
issue.IssueCode,
396+
issue.IssueText,
397+
category,
398+
severity,
399+
})
394400
}
395-
396-
severity := formatSeverity(issue.IssueSeverity)
397-
398-
data = append(data, []string{
399-
wrapText(issueLocation, colWidths[0]),
400-
wrapText(issue.IssueSource, colWidths[1]),
401-
wrapText(issue.Analyzer.Shortcode, colWidths[2]),
402-
wrapText(issue.IssueCode, colWidths[3]),
403-
wrapText(issue.IssueText, colWidths[4]),
404-
wrapText(issue.IssueCategory, colWidths[5]),
405-
wrapText(severity, colWidths[6]),
406-
})
407401
}
408402

409-
pterm.Println(pterm.Bold.Sprintf("Found %d issue(s)", len(opts.issues)))
410-
pterm.DefaultTable.WithHasHeader().WithBoxed().WithRowSeparator("-").WithHeaderRowSeparator("-").WithData(data).Render()
403+
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
404+
pterm.Printf("\nShowing %d issue(s)\n", len(opts.issues))
411405

412406
return nil
413407
}
@@ -536,15 +530,16 @@ func colorSeverity(sev string, text string) string {
536530
}
537531

538532
func formatSeverity(severity string) string {
533+
humanized := humanizeSeverity(severity)
539534
switch strings.ToUpper(severity) {
540535
case "CRITICAL":
541-
return pterm.Red(severity)
536+
return pterm.Red(humanized)
542537
case "MAJOR":
543-
return pterm.LightRed(severity)
538+
return pterm.LightRed(humanized)
544539
case "MINOR":
545-
return pterm.Yellow(severity)
540+
return pterm.Yellow(humanized)
546541
default:
547-
return severity
542+
return humanized
548543
}
549544
}
550545

@@ -559,52 +554,3 @@ func formatLocation(issue issues.Issue, cwd string) string {
559554
return fmt.Sprintf("%s:%d-%d", filePath, issue.Location.Position.BeginLine, issue.Location.Position.EndLine)
560555
}
561556

562-
func wrapText(text string, maxWidth int) string {
563-
if maxWidth <= 0 {
564-
return text
565-
}
566-
567-
if len(text) <= maxWidth {
568-
return text
569-
}
570-
571-
words := strings.Fields(text)
572-
if len(words) == 0 {
573-
return text
574-
}
575-
576-
var lines []string
577-
var currentLine strings.Builder
578-
579-
for _, word := range words {
580-
if currentLine.Len() > 0 && currentLine.Len()+len(word)+1 > maxWidth {
581-
lines = append(lines, currentLine.String())
582-
currentLine.Reset()
583-
}
584-
585-
if len(word) > maxWidth {
586-
if currentLine.Len() > 0 {
587-
lines = append(lines, currentLine.String())
588-
currentLine.Reset()
589-
}
590-
for len(word) > maxWidth {
591-
lines = append(lines, word[:maxWidth])
592-
word = word[maxWidth:]
593-
}
594-
if len(word) > 0 {
595-
currentLine.WriteString(word)
596-
}
597-
} else {
598-
if currentLine.Len() > 0 {
599-
currentLine.WriteString(" ")
600-
}
601-
currentLine.WriteString(word)
602-
}
603-
}
604-
605-
if currentLine.Len() > 0 {
606-
lines = append(lines, currentLine.String())
607-
}
608-
609-
return strings.Join(lines, "\n")
610-
}

command/metrics/metrics.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type MetricsOptions struct {
3636

3737
func NewCmdMetrics() *cobra.Command {
3838
opts := MetricsOptions{
39-
OutputFormat: "human",
39+
OutputFormat: "pretty",
4040
LimitArg: 30,
4141
}
4242

@@ -77,7 +77,7 @@ func NewCmdMetrics() *cobra.Command {
7777
cmd.Flags().IntVar(&opts.PRNumber, "pr", 0, "Scope to a specific pull request by number")
7878

7979
// --output flag
80-
cmd.Flags().StringVarP(&opts.OutputFormat, "output", "o", "human", "Output format: human, table, json, yaml")
80+
cmd.Flags().StringVarP(&opts.OutputFormat, "output", "o", "pretty", "Output format: pretty, table, json, yaml")
8181

8282
// --output-file flag
8383
cmd.Flags().StringVar(&opts.OutputFile, "output-file", "", "Write output to a file instead of stdout")
@@ -94,7 +94,7 @@ func NewCmdMetrics() *cobra.Command {
9494
})
9595
_ = cmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
9696
return []string{
97-
"human\tHuman-readable grouped output",
97+
"pretty\tPretty-printed grouped output",
9898
"table\tTabular output",
9999
"json\tJSON output",
100100
"yaml\tYAML output",
@@ -420,9 +420,9 @@ func (opts *MetricsOptions) printFooter(count int) {
420420
func formatStatus(status string) string {
421421
switch strings.ToUpper(status) {
422422
case "PASSING":
423-
return pterm.Green(status)
423+
return pterm.Green("Passing")
424424
case "FAILING":
425-
return pterm.Red(status)
425+
return pterm.Red("Failing")
426426
default:
427427
return status
428428
}

command/vulnerabilities/vulnerabilities.go

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type VulnerabilitiesOptions struct {
3737

3838
func NewCmdVulnerabilities() *cobra.Command {
3939
opts := VulnerabilitiesOptions{
40-
OutputFormat: "human",
40+
OutputFormat: "pretty",
4141
LimitArg: 100,
4242
}
4343

@@ -80,7 +80,7 @@ func NewCmdVulnerabilities() *cobra.Command {
8080
cmd.Flags().IntVar(&opts.PRNumber, "pr", 0, "Scope to a specific pull request by number")
8181

8282
// --output flag
83-
cmd.Flags().StringVarP(&opts.OutputFormat, "output", "o", "human", "Output format: human, table, json, yaml")
83+
cmd.Flags().StringVarP(&opts.OutputFormat, "output", "o", "pretty", "Output format: pretty, table, json, yaml")
8484

8585
// --output-file flag
8686
cmd.Flags().StringVar(&opts.OutputFile, "output-file", "", "Write output to a file instead of stdout")
@@ -100,7 +100,7 @@ func NewCmdVulnerabilities() *cobra.Command {
100100
})
101101
_ = cmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
102102
return []string{
103-
"human\tHuman-readable grouped output",
103+
"pretty\tPretty-printed grouped output",
104104
"table\tTabular output",
105105
"json\tJSON output",
106106
"yaml\tYAML output",
@@ -340,7 +340,7 @@ func (opts *VulnerabilitiesOptions) outputTable() error {
340340
}
341341

342342
// Build vulnerabilities table
343-
header := []string{"ID", "SEVERITY", "PACKAGE", "VERSION", "FIX", "REACHABLE"}
343+
header := []string{"ID", "SEVERITY", "PACKAGE", "VERSION", "ECOSYSTEM", "FIX", "REACHABILITY"}
344344
data := [][]string{header}
345345

346346
for _, v := range vulnsList {
@@ -351,12 +351,14 @@ func (opts *VulnerabilitiesOptions) outputTable() error {
351351

352352
reachable := formatReachability(v.Reachability)
353353
severity := formatSeverity(v.Vulnerability.Severity)
354+
ecosystem := humanizeEcosystem(v.Package.Ecosystem)
354355

355356
data = append(data, []string{
356357
v.Vulnerability.Identifier,
357358
severity,
358359
v.Package.Name,
359360
v.PackageVersion.Version,
361+
ecosystem,
360362
fix,
361363
reachable,
362364
})
@@ -369,28 +371,55 @@ func (opts *VulnerabilitiesOptions) outputTable() error {
369371
}
370372

371373
func formatSeverity(severity string) string {
374+
humanized := humanizeSeverity(severity)
372375
switch strings.ToUpper(severity) {
373376
case "CRITICAL":
374-
return pterm.Red(severity)
377+
return pterm.Red(humanized)
375378
case "HIGH":
376-
return pterm.LightRed(severity)
379+
return pterm.LightRed(humanized)
377380
case "MEDIUM":
378-
return pterm.Yellow(severity)
381+
return pterm.Yellow(humanized)
379382
case "LOW":
380-
return pterm.Blue(severity)
383+
return pterm.Blue(humanized)
381384
default:
382-
return severity
385+
return humanized
383386
}
384387
}
385388

386389
func formatReachability(reachability string) string {
387390
switch strings.ToUpper(reachability) {
388391
case "REACHABLE":
389-
return pterm.Red("YES")
392+
return pterm.Red("Yes")
390393
case "UNREACHABLE":
391-
return pterm.Green("NO")
394+
return pterm.Green("No")
392395
default:
393-
return "UNKNOWN"
396+
return "Unknown"
397+
}
398+
}
399+
400+
func humanizeEcosystem(ecosystem string) string {
401+
switch strings.ToUpper(ecosystem) {
402+
case "GO":
403+
return "Go"
404+
case "NPM":
405+
return "npm"
406+
case "PYPI":
407+
return "PyPI"
408+
case "MAVEN":
409+
return "Maven"
410+
case "RUBYGEMS":
411+
return "RubyGems"
412+
case "NUGET":
413+
return "NuGet"
414+
case "CARGO":
415+
return "Cargo"
416+
case "PACKAGIST":
417+
return "Packagist"
418+
default:
419+
if ecosystem == "" {
420+
return "-"
421+
}
422+
return ecosystem
394423
}
395424
}
396425

0 commit comments

Comments
 (0)