-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the report command. (#171)
* Changes the -o / --output option to be a file `fossa analyze -o deps.json` for the original behavior you would do `fossa analyze -o -` * Split the report types into sub commands `fossa report licenses -o NOTICE.txt` || `fossa report dependencies --output deps.json` * Added the -t / --template option to process the output via a template file first. `fossa report dependencies --output deps.txt --template deps.tmpl` Default template for licenses report is ``` # 3rd-Party Software License Notice Generated by fossa-cli (https://github.com/fossas/fossa-cli). This software includes the following software and licenses: {{range $license, $deps := .}} ======================================================================== {{$license}} ======================================================================== The following software have components provided under the terms of this license: {{range $i, $dep := $deps}} - {{$dep.Project.Title}} (from {{$dep.Project.URL}}) {{- end}} {{end}} ``` which will produce the same output as before. Since `fossa analyze -o deps.json` && `fossa report dependencies --output deps.json` should output the same info should analyze still have the `-o` or should people just use `fossa report dependencies`?
- Loading branch information
1 parent
d938632
commit fcec296
Showing
15 changed files
with
521 additions
and
231 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package fossa | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/fossas/fossa-cli/pkg" | ||
) | ||
|
||
type License struct { | ||
ID int64 `json:"id"` | ||
LicenseID string `json:"licenseId"` | ||
RevisionID string `json:"revisionId"` | ||
LicenseGroupID int64 `json:"licenseGroupId"` | ||
Ignored bool `json:"ignored"` | ||
Title string `json:"title"` | ||
URL string `json:"url"` | ||
FullText string `json:"text"` | ||
Copyright string `json:"copyright"` | ||
} | ||
|
||
type Revision struct { | ||
Locator *Locator `json:"loc"` | ||
Licenses Licenses `json:"licenses"` | ||
Project *Project `json:"project"` | ||
} | ||
|
||
type Project struct { | ||
Title string `json:"title"` | ||
URL string `json:"url"` | ||
Public bool `json:"public"` | ||
Authors []string `json:"authors"` | ||
} | ||
|
||
type Licenses = []*License | ||
type Revisions = []*Revision | ||
|
||
func FetchRevisionForPackage(p pkg.Package) (rev *Revision, err error) { | ||
locator := LocatorOf(p.ID) | ||
ep := serverURL | ||
ep, _ = ep.Parse("/api/revisions/" + url.PathEscape(locator.QueryString())) | ||
_, err = GetJSON(ep.String(), &rev) | ||
|
||
if err != nil { | ||
return rev, err | ||
} | ||
|
||
if rev.Locator == nil { | ||
rev.Locator = locator | ||
} | ||
if len(rev.Licenses) == 0 { | ||
rev.Licenses = append(rev.Licenses, &License{ | ||
LicenseID: "UNKNOWN", | ||
}) | ||
} | ||
|
||
if rev.Project == nil { | ||
rev.Project = &Project{ | ||
Title: rev.Locator.Project, | ||
URL: "UNKNOWN", | ||
} | ||
} | ||
return rev, err | ||
} | ||
|
||
func FetchRevisionForDeps(deps map[pkg.ID]pkg.Package) (revs Revisions, err error) { | ||
pkgs := make([]string, 0, len(deps)) | ||
for pkgID := range deps { | ||
pkgs = append(pkgs, LocatorOf(pkgID).QueryString()) | ||
} | ||
|
||
// Split pkgs into chunks of 20 for performance reasons | ||
chunks := make([][]string, 0) | ||
chunkSize := 20 | ||
for i := 0; i < len(pkgs); i += chunkSize { | ||
end := i + chunkSize | ||
|
||
if end > len(pkgs) { | ||
end = len(pkgs) | ||
} | ||
|
||
chunks = append(chunks, pkgs[i:end]) | ||
} | ||
|
||
ch := make(chan Revisions, len(chunks)) // buffered | ||
for _, chunk := range chunks { | ||
qs := url.Values{} | ||
for _, q := range chunk { | ||
qs.Add("locator", q) | ||
} | ||
ep := serverURL | ||
if ep, err = ep.Parse("/api/revisions?" + qs.Encode()); err != nil { | ||
return revs, err | ||
} | ||
|
||
go func(url string) { | ||
var ret Revisions | ||
if _, err := GetJSON(ep.String(), &ret); err != nil { | ||
close(ch) | ||
} | ||
ch <- ret | ||
}(ep.String()) | ||
} | ||
|
||
revs = make(Revisions, 0) | ||
for range chunks { | ||
select { | ||
case ret := <-ch: | ||
revs = append(revs, ret...) | ||
} | ||
} | ||
|
||
return revs, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package report | ||
|
||
import ( | ||
"text/template" | ||
|
||
"github.com/fossas/fossa-cli/cmd/fossa/cmdutil" | ||
"github.com/fossas/fossa-cli/cmd/fossa/flags" | ||
"github.com/fossas/fossa-cli/log" | ||
"github.com/fossas/fossa-cli/pkg" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
var dependenciesCmd = cli.Command{ | ||
Name: "dependencies", | ||
Usage: "Generate dependencies report", | ||
Flags: flags.WithGlobalFlags(flags.WithAPIFlags(flags.WithModulesFlags(flags.WithReportTemplateFlags([]cli.Flag{})))), | ||
Before: prepareReportCtx, | ||
Action: generateDependencies, | ||
} | ||
|
||
func generateDependencies(ctx *cli.Context) (err error) { | ||
pkgs := make([]pkg.Package, 0) | ||
for _, module := range analyzed { | ||
for _, pkg := range module.Deps { | ||
pkgs = append(pkgs, pkg) | ||
} | ||
} | ||
|
||
var tmpl *template.Template | ||
|
||
if ctx.String(flags.Template) != "" { | ||
tmpl, err = template.ParseFiles(ctx.String(flags.Template)) | ||
if err != nil { | ||
log.Logger.Fatalf("Could not parse template data: %s", err.Error()) | ||
} | ||
} | ||
|
||
return cmdutil.OutputData(ctx.String(flags.ShowOutput), tmpl, pkgs) | ||
} |
Oops, something went wrong.