-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cmd): Use IO services for effects [WIP]
- Loading branch information
Showing
36 changed files
with
2,348 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/fossas/fossa-cli/module" | ||
) | ||
|
||
type SourceUnit struct { | ||
Name string | ||
Type string | ||
Manifest string | ||
Build Build | ||
} | ||
|
||
type Build struct { | ||
Artifact string | ||
Context interface{} | ||
|
||
Succeeded bool | ||
Error error `json:",omitempty"` | ||
|
||
Dependencies []Dependency | ||
} | ||
|
||
type Dependency struct { | ||
// Location | ||
Locator string `json:"locator"` | ||
|
||
// Metadata | ||
Data *json.RawMessage `json:"data,omitempty"` | ||
|
||
// Context | ||
Depth int `json:"depth,omitempty"` | ||
Parent string `json:"parent,omitempty"` | ||
UnresolvedLocators []string `json:"unresolved_locators,omitempty"` | ||
} | ||
|
||
func NormalizeType(t module.Type) (string, error) { | ||
// TODO: handle more normalizations | ||
switch t { | ||
case module.Nodejs: | ||
return "commonjspackage", nil | ||
} | ||
return "", errors.Errorf("unknown module type: %s", string(t)) | ||
} | ||
|
||
func Normalize(analysis []module.Analyzed) ([]SourceUnit, error) { | ||
var normalized []SourceUnit | ||
for _, analyzed := range analysis { | ||
var deps []Dependency | ||
for _, dep := range analyzed.Dependencies { | ||
data, err := json.Marshal(dep) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not marshal analyzed dependency") | ||
} | ||
|
||
deps = append(deps, Dependency{ | ||
Locator: string(module.LocatorOf(dep)), | ||
Data: (*json.RawMessage)(&data), | ||
}) | ||
} | ||
|
||
normalizedType, err := NormalizeType(analyzed.Module.Type) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not normalize analyzed module type") | ||
} | ||
|
||
normalized = append(normalized, SourceUnit{ | ||
Name: analyzed.Module.Name, | ||
Type: normalizedType, | ||
Manifest: analyzed.Module.Target, | ||
Build: Build{ | ||
Artifact: "default", | ||
Context: analyzed.Builder, | ||
Succeeded: true, | ||
Dependencies: deps, | ||
}, | ||
}) | ||
} | ||
return normalized, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package analyze | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/urfave/cli" | ||
|
||
"github.com/fossas/fossa-cli/config" | ||
"github.com/fossas/fossa-cli/module" | ||
"github.com/fossas/fossa-cli/services" | ||
) | ||
|
||
func Run(c *cli.Context) { | ||
conf := config.MustNew(c) | ||
io := services.New(false, false) | ||
|
||
if len(conf.Modules) == 0 { | ||
io.Logger.Fatalf("No modules specified.") | ||
} | ||
|
||
analysis, err := doAnalyze(conf.Modules, conf.AnalyzeCmd.AllowUnresolved) | ||
if err != nil { | ||
io.Logger.Fatalf("Analysis failed: %s", err.Error()) | ||
} | ||
io.Logger.Debugf("Analysis complete: %#v", analysis) | ||
|
||
normalModules, err := normalizeAnalysis(analysis) | ||
if err != nil { | ||
io.Logger.Fatalf("Could not normalize build data: %s", err.Error()) | ||
} | ||
|
||
if conf.AnalyzeCmd.Output { | ||
buildData, err := json.Marshal(normalModules) | ||
if err != nil { | ||
io.Logger.Fatalf("Could not marshal analysis results: %s", err.Error()) | ||
} | ||
fmt.Println(string(buildData)) | ||
os.Exit(0) | ||
return | ||
} | ||
|
||
msg, err := doUpload(conf, normalModules) | ||
if err != nil { | ||
io.Logger.Fatalf("Upload failed: %s", err.Error()) | ||
} | ||
fmt.Print(msg) | ||
} | ||
|
||
type analysisKey struct { | ||
builder module.Builder | ||
module module.Module | ||
} | ||
|
||
type analysis map[analysisKey][]module.Dependency | ||
|
||
func Do(modules []module.Config, allowUnresolved bool) (analysis, error) { | ||
io.Logger.Debugf("Running analysis on modules: %#v", modules) | ||
dependencies := make(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) | ||
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) | ||
} | ||
|
||
deps, err := builder.Analyze(m, allowUnresolved) | ||
if err != nil { | ||
return nil, fmt.Errorf("analysis failed on module %s: %s", m.Name, err.Error()) | ||
} | ||
dependencies[analysisKey{ | ||
builder: builder, | ||
module: m, | ||
}] = deps | ||
} | ||
|
||
return dependencies, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package common | ||
|
||
import "github.com/urfave/cli" | ||
|
||
var GlobalFlags = []cli.Flag{ | ||
cli.BoolFlag{Name: "debug", Usage: "print debug information to stderr"}, | ||
cli.BoolFlag{Name: "no-ansi", Usage: "do not use interactive mode (ANSI codes)"}, | ||
} | ||
|
||
func WithGlobalFlags(flags []cli.Flag) []cli.Flag { | ||
return append(flags, GlobalFlags...) | ||
} |
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,19 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/urfave/cli" | ||
|
||
"github.com/fossas/fossa-cli/config" | ||
"github.com/fossas/fossa-cli/services" | ||
) | ||
|
||
func MustInit(ctx *cli.Context) (services.Services, config.CLIConfig) { | ||
c := config.MustNew(ctx) | ||
io := services.New(c.Interactive, c.Debug) | ||
err := io.API.Initialize(c.Endpoint, c.APIKey) | ||
if err != nil { | ||
io.Logger.Fatalf("Could not initialize API: %s", err.Error()) | ||
} | ||
|
||
return io, c | ||
} |
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
Oops, something went wrong.