-
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.
WIP: analysis command refactor complete
- Loading branch information
Showing
13 changed files
with
266 additions
and
188 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -10,4 +10,4 @@ analyze: | |
modules: | ||
- name: fossa | ||
path: cmd/fossa | ||
type: Go | ||
type: go |
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 |
---|---|---|
@@ -1,93 +1,77 @@ | ||
package main | ||
package analyze | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/urfave/cli" | ||
|
||
"github.com/fossas/fossa-cli/analyzers" | ||
"github.com/fossas/fossa-cli/api/fossa" | ||
"github.com/fossas/fossa-cli/cmd/fossa/cmdutil" | ||
"github.com/fossas/fossa-cli/cmd/fossa/flags" | ||
"github.com/fossas/fossa-cli/config" | ||
"github.com/fossas/fossa-cli/log" | ||
"github.com/fossas/fossa-cli/module" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func analyzeCmd(c *cli.Context) { | ||
conf, err := config.New(c) | ||
if err != nil { | ||
log.Logger.Fatalf("Could not load configuration: %s", err.Error()) | ||
} | ||
if len(conf.Modules) == 0 { | ||
log.Logger.Fatal("No modules specified.") | ||
} | ||
var ( | ||
Output = "output" | ||
) | ||
|
||
analyses, err := doAnalyze(conf.Modules, conf.AnalyzeCmd.AllowUnresolved) | ||
if err != nil { | ||
log.Logger.Fatalf("Analysis failed: %s", err.Error()) | ||
} | ||
var Cmd = cli.Command{ | ||
Name: "analyze", | ||
Usage: "Analyze built dependencies", | ||
Action: Run, | ||
ArgsUsage: "MODULE", | ||
Flags: flags.WithGlobalFlags(flags.WithAPIFlags(flags.WithModulesFlags([]cli.Flag{ | ||
cli.BoolFlag{Name: flags.Short(Output), Usage: "show analysis output instead of uploading to FOSSA"}, | ||
}))), | ||
} | ||
|
||
log.Logger.Debugf("Analysis complete: %#v", analyses) | ||
var _ cli.ActionFunc = Run | ||
|
||
normalModules, err := normalizeAnalysis(analyses) | ||
func Run(ctx *cli.Context) error { | ||
err := cmdutil.Init(ctx) | ||
if err != nil { | ||
log.Logger.Fatalf("Could not normalize build data: %s", err.Error()) | ||
} | ||
|
||
if conf.AnalyzeCmd.Output { | ||
buildData, err := json.Marshal(normalModules) | ||
if err != nil { | ||
log.Logger.Fatalf("Could not marshal analysis results: %s", err.Error()) | ||
} | ||
fmt.Println(string(buildData)) | ||
os.Exit(0) | ||
return | ||
log.Logger.Fatalf("Could not initialize: %s", err.Error()) | ||
} | ||
|
||
msg, err := doUpload(conf, normalModules) | ||
modules, err := config.Modules() | ||
if err != nil { | ||
log.Logger.Fatalf("Upload failed: %s", err.Error()) | ||
log.Logger.Fatalf("Could not parse modules: %s", err.Error()) | ||
} | ||
if len(modules) == 0 { | ||
log.Logger.Fatal("No modules specified.") | ||
} | ||
fmt.Print(msg) | ||
} | ||
|
||
type analysis struct { | ||
builder module.Builder | ||
module module.Module | ||
dependencies []module.Dependency | ||
} | ||
|
||
func doAnalyze(modules []module.Config, allowUnresolved bool) ([]analysis, error) { | ||
log.Logger.Debugf("Running analysis on modules: %#v", modules) | ||
analyses := []analysis{} | ||
|
||
for _, moduleConfig := range modules { | ||
builder, m, err := resolveModuleConfig(moduleConfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to resolve modules: %s", err.Error()) | ||
} | ||
|
||
err = builder.Initialize() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to initialize build: %s", err.Error()) | ||
} | ||
|
||
isBuilt, err := builder.IsBuilt(m, allowUnresolved) | ||
var analyzed []module.Module | ||
defer log.StopSpinner() | ||
for i, m := range modules { | ||
log.ShowSpinner(fmt.Sprintf("Analyzing module (%d/%d): %s", i+1, len(modules), m.Name)) | ||
analyzer, err := analyzers.New(m.Type, m.Options) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not determine whether module %#v is built: %#v", m.Name, err.Error()) | ||
} | ||
if !isBuilt { | ||
return nil, fmt.Errorf("module %s does not appear to be built (try first running your build or `fossa build`, and then running `fossa`)", m.Name) | ||
log.Logger.Warningf("Could not load analyzer: %s", err.Error()) | ||
} | ||
|
||
deps, err := builder.Analyze(m, allowUnresolved) | ||
result, err := analyzer.Analyze(m) | ||
if err != nil { | ||
return nil, fmt.Errorf("analysis failed on module %s: %s", m.Name, err.Error()) | ||
log.Logger.Warningf("Could not analyze: %s", err.Error()) | ||
} | ||
analyses = append(analyses, analysis{ | ||
builder: builder, | ||
module: m, | ||
dependencies: deps, | ||
}) | ||
analyzed = append(analyzed, result) | ||
} | ||
log.StopSpinner() | ||
|
||
return analyses, nil | ||
normalized, err := fossa.Normalize(analyzed) | ||
if err != nil { | ||
log.Logger.Fatalf("Could not normalize output: %s", err.Error()) | ||
} | ||
if ctx.Bool(Output) { | ||
log.Printf("%#v", normalized) | ||
return nil | ||
} | ||
locator, err := fossa.Upload(config.Fetcher(), config.Project(), config.Revision(), config.Title(), config.Branch(), normalized) | ||
if err != nil { | ||
log.Logger.Fatalf("Error during upload: %s", err.Error()) | ||
} | ||
log.Printf(cmdutil.FmtReportURL(locator)) | ||
return nil | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package test | ||
|
||
import ( | ||
"encoding/json" | ||
|
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
Oops, something went wrong.