Skip to content

Commit

Permalink
feat: add CSV output option
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Phillips <[email protected]>
  • Loading branch information
spiffcs committed Mar 21, 2024
1 parent 9b51738 commit d6db34f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
7 changes: 6 additions & 1 deletion cmd/grant/cli/internal/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ type Format string
const (
JSON Format = "json"
Table Format = "table"
CSV Format = "csv"
)

var ValidFormats = []Format{JSON, Table}
var ValidFormats = []Format{JSON, Table, CSV}

// ValidateFormat returns a valid format or the default format if the given format is invalid
func ValidateFormat(f Format) Format {
Expand All @@ -22,6 +23,8 @@ func ValidateFormat(f Format) Format {
return JSON
case "table":
return Table
case "csv":
return CSV
default:
return Table
}
Expand Down Expand Up @@ -58,6 +61,7 @@ func NewLicense(l grant.License) License {
type Package struct {
Name string `json:"name" yaml:"name"`
Version string `json:"version" yaml:"version"`
Type string `json:"type" yaml:"type"`
Locations []string `json:"locations" yaml:"locations"`
}

Expand All @@ -69,6 +73,7 @@ func NewPackage(p *grant.Package) Package {
Name: p.Name,
Version: p.Version,
Locations: p.Locations,
Type: p.Type,
}
}

Expand Down
45 changes: 43 additions & 2 deletions cmd/grant/cli/internal/list/report.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package list

import (
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"os"
"slices"
"time"

Expand Down Expand Up @@ -55,6 +57,8 @@ func (r *Report) Render() error {
return r.renderList()
case internal.JSON:
return r.renderJSON()
case internal.CSV:
return r.renderCSV()
default:
r.errors = append(r.errors, fmt.Errorf("invalid format: %s; valid formats are: %s", r.Config.Options.Format, internal.ValidFormats))
return errors.Join(r.errors...)
Expand Down Expand Up @@ -84,7 +88,40 @@ func NewResult(input string, gl grant.License, gp ...*grant.Package) Result {
}
}

func (r *Report) renderJSON() error {
func (r *Report) renderCSV() error {
response := getResponse(r)
headers := []string{"component", "component_version", "license", "website", "type"}
data := [][]string{
headers,
}

for _, rslt := range response.Results {
for _, pkg := range rslt.Packages {
data = append(data, []string{
pkg.Name,
pkg.Version,
rslt.License.Name,
rslt.License.Reference,
pkg.Type,
})
}
}

writer := csv.NewWriter(os.Stdout)
defer writer.Flush()

for _, record := range data {
if err := writer.Write(record); err != nil {
return err
}
}
if err := writer.Error(); err != nil {

Check failure on line 118 in cmd/grant/cli/internal/list/report.go

View workflow job for this annotation

GitHub Actions / Validations

if-return: redundant if ...; err != nil check, just return error instead. (revive)
return err
}
return nil
}

func getResponse(r *Report) Response {
resp := Response{
ReportID: r.ReportID,
Timestamp: r.Timestamp,
Expand All @@ -94,14 +131,18 @@ func (r *Report) renderJSON() error {

for _, c := range r.Cases {
resp.Inputs = append(resp.Inputs, c.UserInput)
// TODO: is it better to invert this here and grab packages -> licenses since package is the cases first class
licensePackages, licenses, _ := c.GetLicenses()
for key, l := range licenses {
packages := licensePackages[key]
result := NewResult(c.UserInput, l, packages...)
resp.Results = append(resp.Results, result)
}
}
return resp
}

func (r *Report) renderJSON() error {
resp := getResponse(r)
jsonData, err := json.Marshal(resp)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions grant/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type PackageID string
type Package struct {
ID PackageID `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Type string `json:"type" yaml:"type"`
Version string `json:"version" yaml:"version"`
Licenses []License `json:"licenses" yaml:"licenses"`
Locations []string `json:"locations" yaml:"locations"`
Expand All @@ -25,6 +26,7 @@ func ConvertSyftPackage(p syftPkg.Package) *Package {
return &Package{
Name: p.Name,
Version: p.Version,
Type: string(p.Type),
Licenses: ConvertSyftLicenses(p.Licenses),
Locations: packageLocations,
}
Expand Down

0 comments on commit d6db34f

Please sign in to comment.