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

Feature/support color op for scoring #350

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions cmd/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type userCmd struct {
json bool
basic bool
detailed bool
color bool

// directory control
recurse bool
Expand Down Expand Up @@ -156,6 +157,7 @@ func toUserCmd(cmd *cobra.Command, args []string) *userCmd {
uCmd.json, _ = cmd.Flags().GetBool("json")
uCmd.basic, _ = cmd.Flags().GetBool("basic")
uCmd.detailed, _ = cmd.Flags().GetBool("detailed")
uCmd.color, _ = cmd.Flags().GetBool("color")

if reportFormat != "" {
uCmd.json = strings.ToLower(reportFormat) == "json"
Expand All @@ -177,6 +179,7 @@ func toEngineParams(uCmd *userCmd) *engine.Params {
JSON: uCmd.json,
Basic: uCmd.basic,
Detailed: uCmd.detailed,
Color: uCmd.color,
Recurse: uCmd.recurse,
Debug: uCmd.debug,
ConfigPath: uCmd.configPath,
Expand Down Expand Up @@ -241,6 +244,7 @@ func init() {
scoreCmd.Flags().BoolP("json", "j", false, "results in json")
scoreCmd.Flags().BoolP("detailed", "d", false, "results in table format, default")
scoreCmd.Flags().BoolP("basic", "b", false, "results in single line format")
scoreCmd.Flags().BoolP("color", "l", false, "output in colorful")

// Debug Control
scoreCmd.Flags().BoolP("debug", "D", false, "enable debug logging")
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand All @@ -36,6 +38,7 @@ require (
github.com/anchore/go-struct-converter v0.0.0-20240925125616-a0883641c664 // indirect
github.com/cloudflare/circl v1.5.0 // indirect
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
github.com/fatih/color v1.18.0
github.com/go-git/go-billy/v5 v5.6.0
github.com/google/go-querystring v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/github/go-spdx/v2 v2.3.2 h1:IfdyNHTqzs4zAJjXdVQfRnxt1XMfycXoHBE2Vsm1bjs=
github.com/github/go-spdx/v2 v2.3.2/go.mod h1:2ZxKsOhvBp+OYBDlsGnUMcchLeo2mrpEBn2L1C+U3IQ=
github.com/go-git/go-billy/v5 v5.6.0 h1:w2hPNtoehvJIxR00Vb4xX94qHQi/ApZfX+nBE2Cjio8=
Expand All @@ -43,6 +45,11 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down Expand Up @@ -133,6 +140,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
4 changes: 2 additions & 2 deletions pkg/compliance/bsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const (
SBOM_VULNERABILITES
)

func bsiResult(ctx context.Context, doc sbom.Document, fileName string, outFormat string) {
func bsiResult(ctx context.Context, doc sbom.Document, fileName string, outFormat string, colorOutput bool) {
log := logger.FromContext(ctx)
log.Debug("compliance.bsiResult()")

Expand All @@ -107,7 +107,7 @@ func bsiResult(ctx context.Context, doc sbom.Document, fileName string, outForma
}

if outFormat == "detailed" {
bsiDetailedReport(dtb, fileName)
bsiDetailedReport(dtb, fileName, colorOutput)
}
}

Expand Down
23 changes: 20 additions & 3 deletions pkg/compliance/bsi_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/interlynk-io/sbomqs/pkg/compliance/common"
db "github.com/interlynk-io/sbomqs/pkg/compliance/db"
"github.com/olekukonko/tablewriter"
"sigs.k8s.io/release-utils/version"
Expand Down Expand Up @@ -173,7 +174,7 @@ func constructSections(dtb *db.DB) []bsiSection {
return sortedSections
}

func bsiDetailedReport(dtb *db.DB, fileName string) {
func bsiDetailedReport(dtb *db.DB, fileName string, colorOutput bool) {
table := tablewriter.NewWriter(os.Stdout)
score := bsiAggregateScore(dtb)

Expand All @@ -189,9 +190,25 @@ func bsiDetailedReport(dtb *db.DB, fileName string) {
for _, section := range sections {
sectionID := section.ID
if !section.Required {
sectionID = sectionID + "*"
sectionID += "*"
}

if colorOutput {
// disable tablewriter's auto-wrapping
table.SetAutoWrapText(false)
columnWidth := 30
common.SetHeaderColor(table, 5)

table = common.ColorTable(table,
section.ElementID,
section.ID,
section.ElementResult,
section.DataField,
section.Score,
columnWidth)
} else {
table.Append([]string{section.ElementID, sectionID, section.DataField, section.ElementResult, fmt.Sprintf("%0.1f", section.Score)})
}
table.Append([]string{section.ElementID, sectionID, section.DataField, section.ElementResult, fmt.Sprintf("%0.1f", section.Score)})
}
table.Render()
}
Expand Down
73 changes: 73 additions & 0 deletions pkg/compliance/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package common

import (
"fmt"
"path"
"strings"
"time"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/interlynk-io/sbomqs/pkg/sbom"
"github.com/interlynk-io/sbomqs/pkg/swhid"
"github.com/interlynk-io/sbomqs/pkg/swid"
"github.com/olekukonko/tablewriter"
"github.com/samber/lo"
)

Expand Down Expand Up @@ -375,3 +377,74 @@ func IsComponentPartOfPrimaryDependency(primaryCompDeps []string, comp string) b
}
return false
}

func SetHeaderColor(table *tablewriter.Table, header int) {
colors := make([]tablewriter.Colors, header)

// each column with same color and style
for i := 0; i < header; i++ {
colors[i] = tablewriter.Colors{tablewriter.FgHiWhiteColor, tablewriter.Bold}
}

table.SetHeaderColor(colors...)
}

func ColorTable(table *tablewriter.Table, elementID, id string, elementResult string, dataFields string, score float64, columnWidth int) *tablewriter.Table {
elementRe := wrapAndColoredContent(elementResult, columnWidth, tablewriter.FgHiCyanColor)
dataField := wrapAndColoredContent(dataFields, columnWidth, tablewriter.FgHiBlueColor)

scoreColor := GetScoreColor(score)

table.Rich([]string{
elementID,
id,
dataField,
elementRe,
fmt.Sprintf("%0.1f", score),
}, []tablewriter.Colors{
{tablewriter.FgHiMagentaColor, tablewriter.Bold},
{tablewriter.FgHiCyanColor},
{},
{},
scoreColor,
})
return table
}

// custom wrapping function to ensure consistent coloring instead of tablewritter's in-built wrapping
// 1. split content into multiple lines, each fitting within the specified width
// 2. each line of the content is formatted with color and bold styling using ANSI escape codes
// 3. wrapped lines are joined together with newline characters (\n) to maintain proper multi-line formatting.
func wrapAndColoredContent(content string, width int, color int) string {
words := strings.Fields(content)
var wrappedContent []string
var currentLine string

for _, word := range words {
if len(currentLine)+len(word)+1 > width {

// wrap the current line and color it
wrappedContent = append(wrappedContent, fmt.Sprintf("\033[%d;%dm%s\033[0m", 1, color, currentLine))
currentLine = word
} else {
if currentLine != "" {
currentLine += " "
}
currentLine += word
}
}
if currentLine != "" {
wrappedContent = append(wrappedContent, fmt.Sprintf("\033[%d;%dm%s\033[0m", 1, color, currentLine))
}

return strings.Join(wrappedContent, "\n")
}

func GetScoreColor(score float64) tablewriter.Colors {
if score == 0.0 {
return tablewriter.Colors{tablewriter.FgRedColor, tablewriter.Bold}
} else if score < 5.0 {
return tablewriter.Colors{tablewriter.FgHiYellowColor, tablewriter.Bold}
}
return tablewriter.Colors{tablewriter.FgGreenColor, tablewriter.Bold}
}
6 changes: 3 additions & 3 deletions pkg/compliance/compliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,20 @@ func ComplianceResult(ctx context.Context, doc sbom.Document, reportType, fileNa

switch {
case reportType == BSI_REPORT:
bsiResult(ctx, doc, fileName, outFormat)
bsiResult(ctx, doc, fileName, outFormat, coloredOutput)

case reportType == BSI_V2_REPORT:
bsiV2Result(ctx, doc, fileName, outFormat)

case reportType == NTIA_REPORT:
ntiaResult(ctx, doc, fileName, outFormat)
ntiaResult(ctx, doc, fileName, outFormat, coloredOutput)

case reportType == OCT_TELCO:
if doc.Spec().GetSpecType() != "spdx" {
fmt.Println("The Provided SBOM spec is other than SPDX. Open Chain Telco only support SPDX specs SBOMs.")
return nil
}
octResult(ctx, doc, fileName, outFormat)
octResult(ctx, doc, fileName, outFormat, coloredOutput)

case reportType == FSCT_V3:
fsct.Result(ctx, doc, fileName, outFormat, coloredOutput)
Expand Down
47 changes: 21 additions & 26 deletions pkg/compliance/fsct/fsct_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/interlynk-io/sbomqs/pkg/compliance/common"
"github.com/interlynk-io/sbomqs/pkg/compliance/db"
"github.com/olekukonko/tablewriter"
"sigs.k8s.io/release-utils/version"
Expand Down Expand Up @@ -199,15 +200,7 @@ func fsctDetailedReport(db *db.DB, fileName string, coloredOutput bool) {
table.SetAutoMergeCellsByColumnIndex([]int{0})

if coloredOutput {
// Set header colors if the colors flag is true
table.SetHeaderColor(
tablewriter.Colors{tablewriter.FgHiWhiteColor, tablewriter.Bold},
tablewriter.Colors{tablewriter.FgHiWhiteColor, tablewriter.Bold},
tablewriter.Colors{tablewriter.FgHiWhiteColor, tablewriter.Bold},
tablewriter.Colors{tablewriter.FgHiWhiteColor, tablewriter.Bold},
tablewriter.Colors{tablewriter.FgHiWhiteColor, tablewriter.Bold},
tablewriter.Colors{tablewriter.FgHiWhiteColor, tablewriter.Bold},
)
common.SetHeaderColor(table, 6)
}

sections := fsctConstructSections(db)
Expand All @@ -218,23 +211,10 @@ func fsctDetailedReport(db *db.DB, fileName string, coloredOutput bool) {
sectionID += "*"
}

// Define maturity color based on the flag
var maturityColor tablewriter.Colors
if coloredOutput {
switch section.Maturity {
case "None":
maturityColor = tablewriter.Colors{tablewriter.FgRedColor, tablewriter.Bold}
case "Minimum":
maturityColor = tablewriter.Colors{tablewriter.FgGreenColor, tablewriter.Bold}
case "Recommended":
maturityColor = tablewriter.Colors{tablewriter.FgCyanColor, tablewriter.Bold}
case "Aspirational":
maturityColor = tablewriter.Colors{tablewriter.FgHiYellowColor, tablewriter.Bold}
}
}

// Use Rich() with color settings only if colors is true
if coloredOutput {
maturityColor := getMaturityColor(section.Maturity)

table.Rich([]string{
section.ElementID,
sectionID,
Expand All @@ -244,9 +224,9 @@ func fsctDetailedReport(db *db.DB, fileName string, coloredOutput bool) {
section.Maturity,
}, []tablewriter.Colors{
{tablewriter.FgHiMagentaColor, tablewriter.Bold},
{},
{tablewriter.FgHiCyanColor},
{tablewriter.FgHiBlueColor, tablewriter.Bold},
{tablewriter.FgHiWhiteColor, tablewriter.Bold},
{tablewriter.FgHiCyanColor, tablewriter.Bold},
maturityColor,
maturityColor,
})
Expand All @@ -269,3 +249,18 @@ func fsctBasicReport(db *db.DB, fileName string) {
fmt.Printf("Framing Software Component Transparency (v3)\n")
fmt.Printf("Score:%0.1f for %s\n", score.totalScore(), fileName)
}

func getMaturityColor(maturity string) tablewriter.Colors {
switch maturity {
case "None":
return tablewriter.Colors{tablewriter.FgRedColor, tablewriter.Bold}
case "Minimum":
return tablewriter.Colors{tablewriter.FgGreenColor, tablewriter.Bold}
case "Recommended":
return tablewriter.Colors{tablewriter.FgCyanColor, tablewriter.Bold}
case "Aspirational":
return tablewriter.Colors{tablewriter.FgHiYellowColor, tablewriter.Bold}
default:
return tablewriter.Colors{}
}
}
4 changes: 2 additions & 2 deletions pkg/compliance/ntia.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
SCORE_ZERO = 0.0
)

func ntiaResult(ctx context.Context, doc sbom.Document, fileName string, outFormat string) {
func ntiaResult(ctx context.Context, doc sbom.Document, fileName string, outFormat string, colorOutput bool) {
log := logger.FromContext(ctx)
log.Debug("compliance.ntiaResult()")

Expand All @@ -59,7 +59,7 @@ func ntiaResult(ctx context.Context, doc sbom.Document, fileName string, outForm
}

if outFormat == "detailed" {
ntiaDetailedReport(db, fileName)
ntiaDetailedReport(db, fileName, colorOutput)
}
}

Expand Down
21 changes: 19 additions & 2 deletions pkg/compliance/ntia_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/interlynk-io/sbomqs/pkg/compliance/common"
"github.com/interlynk-io/sbomqs/pkg/compliance/db"
"github.com/olekukonko/tablewriter"
"sigs.k8s.io/release-utils/version"
Expand Down Expand Up @@ -136,7 +137,7 @@ func ntiaConstructSections(db *db.DB) []ntiaSection {
return sortedSections
}

func ntiaDetailedReport(db *db.DB, fileName string) {
func ntiaDetailedReport(db *db.DB, fileName string, colorOutput bool) {
table := tablewriter.NewWriter(os.Stdout)
score := ntiaAggregateScore(db)

Expand All @@ -162,7 +163,23 @@ func ntiaDetailedReport(db *db.DB, fileName string) {
if !section.Required {
sectionID = sectionID + "*"
}
table.Append([]string{section.ElementID, sectionID, section.DataField, section.ElementResult, fmt.Sprintf("%0.1f", section.Score)})

if colorOutput {
// disable tablewriter's auto-wrapping
table.SetAutoWrapText(false)
columnWidth := 30
common.SetHeaderColor(table, 5)

table = common.ColorTable(table,
section.ElementID,
section.ID,
section.ElementResult,
section.DataField,
section.Score,
columnWidth)
} else {
table.Append([]string{section.ElementID, sectionID, section.DataField, section.ElementResult, fmt.Sprintf("%0.1f", section.Score)})
}
}
table.Render()
}
Expand Down
Loading