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

feat: add CSV output option #65

Merged
merged 2 commits into from
Mar 21, 2024
Merged
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
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
43 changes: 41 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,38 @@ 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
}
}

return writer.Error()
}

func getResponse(r *Report) Response {
resp := Response{
ReportID: r.ReportID,
Timestamp: r.Timestamp,
Expand All @@ -94,14 +129,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
Loading