Skip to content

Commit

Permalink
Refactor module functionality (#100)
Browse files Browse the repository at this point in the history
* refactor: Refactor module functionality into packages

* lint: Run gofmt -s
  • Loading branch information
elldritch committed Mar 13, 2018
1 parent 6600859 commit c19b51b
Show file tree
Hide file tree
Showing 24 changed files with 331 additions and 291 deletions.
5 changes: 2 additions & 3 deletions builders/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/bmatcuk/doublestar"
logging "github.com/op/go-logging"

"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
)

Expand Down Expand Up @@ -126,6 +125,6 @@ func (builder *VendoredArchiveBuilder) IsModule(target string) (bool, error) {
}

// DiscoverModules is not implemented for VendoredArchiveBuilder, instead it must be explicitly configured
func (builder *VendoredArchiveBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, error) {
return []config.ModuleConfig{}, nil
func (builder *VendoredArchiveBuilder) DiscoverModules(dir string) ([]module.Config, error) {
return []module.Config{}, nil
}
9 changes: 4 additions & 5 deletions builders/bower.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/bmatcuk/doublestar"
logging "github.com/op/go-logging"

"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
)

Expand Down Expand Up @@ -143,8 +142,8 @@ func (builder *BowerBuilder) IsModule(target string) (bool, error) {
}

// DiscoverModules finds any bower.json modules not in node_modules or bower_components folders
func (builder *BowerBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, error) {
var moduleConfigs []config.ModuleConfig
func (builder *BowerBuilder) DiscoverModules(dir string) ([]module.Config, error) {
var moduleConfigs []module.Config
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
bowerLogger.Debugf("Failed to access path %s: %s", path, err.Error())
Expand All @@ -166,10 +165,10 @@ func (builder *BowerBuilder) DiscoverModules(dir string) ([]config.ModuleConfig,
}

bowerLogger.Debugf("Found Bower package: %s (%s)", path, moduleName)
moduleConfigs = append(moduleConfigs, config.ModuleConfig{
moduleConfigs = append(moduleConfigs, module.Config{
Name: moduleName,
Path: path,
Type: string(config.Bower),
Type: string(module.Bower),
})
}
return nil
Expand Down
23 changes: 11 additions & 12 deletions builders/builder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package builders

import (
"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
)

Expand All @@ -20,29 +19,29 @@ type Builder interface {
// IsModule takes a string like and returns whether it matches to elect this Builder.
IsModule(configKey string) (bool, error)
// DiscoverModules finds what modules are available for analysis in a given directory.
DiscoverModules(dir string) ([]config.ModuleConfig, error)
DiscoverModules(dir string) ([]module.Config, error)
}

// New instantiates a Builder given a ModuleType
func New(moduleType config.ModuleType) Builder {
func New(moduleType module.Type) Builder {
switch moduleType {
case config.Bower:
case module.Bower:
return &BowerBuilder{}
case config.Composer:
case module.Composer:
return &ComposerBuilder{}
case config.Golang:
case module.Golang:
return &GoBuilder{}
case config.Gradle:
case module.Gradle:
return &GradleBuilder{}
case config.Maven:
case module.Maven:
return &MavenBuilder{}
case config.Nodejs:
case module.Nodejs:
return &NodeJSBuilder{}
case config.Ruby:
case module.Ruby:
return &RubyBuilder{}
case config.SBT:
case module.SBT:
return &SBTBuilder{}
case config.VendoredArchives:
case module.VendoredArchives:
return &VendoredArchiveBuilder{}
}
return nil
Expand Down
9 changes: 4 additions & 5 deletions builders/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

logging "github.com/op/go-logging"

"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
)

Expand Down Expand Up @@ -140,8 +139,8 @@ func (builder *ComposerBuilder) IsModule(target string) (bool, error) {
}

// DiscoverModules finds composer.json modules not a /vendor/ folder
func (builder *ComposerBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, error) {
var moduleConfigs []config.ModuleConfig
func (builder *ComposerBuilder) DiscoverModules(dir string) ([]module.Config, error) {
var moduleConfigs []module.Config
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
composerLogger.Debugf("failed to access path %s: %s\n", path, err.Error())
Expand All @@ -164,10 +163,10 @@ func (builder *ComposerBuilder) DiscoverModules(dir string) ([]config.ModuleConf
}

nodejsLogger.Debugf("found Compower package: %s (%s)", path, moduleName)
moduleConfigs = append(moduleConfigs, config.ModuleConfig{
moduleConfigs = append(moduleConfigs, module.Config{
Name: moduleName,
Path: path,
Type: string(config.Composer),
Type: string(module.Composer),
})
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions builders/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
logging "github.com/op/go-logging"
yaml "gopkg.in/yaml.v2"

"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
)

Expand Down Expand Up @@ -735,8 +734,8 @@ func (builder *GoBuilder) IsModule(target string) (bool, error) {
}

// DiscoverModules walks subdirectories for a Go file with `package main`.
func (builder *GoBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, error) {
var modules []config.ModuleConfig
func (builder *GoBuilder) DiscoverModules(dir string) ([]module.Config, error) {
var modules []module.Config
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
goLogger.Debugf("Failed to access path %s: %s", path, err.Error())
Expand All @@ -763,7 +762,7 @@ func (builder *GoBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, er
if err != nil {
return fmt.Errorf("could not compute module path: %s", err.Error())
}
modules = append(modules, config.ModuleConfig{
modules = append(modules, module.Config{
Name: info.Name(),
Path: modulePath,
Type: "go",
Expand Down
11 changes: 5 additions & 6 deletions builders/gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

logging "github.com/op/go-logging"

"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
)

Expand Down Expand Up @@ -108,7 +107,7 @@ func (builder *GradleBuilder) IsModule(target string) (bool, error) {
}

// DiscoverModules finds either a root build.gradle file in the specified dir
func (builder *GradleBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, error) {
func (builder *GradleBuilder) DiscoverModules(dir string) ([]module.Config, error) {
if err := builder.Initialize(); err != nil {
return nil, err
}
Expand All @@ -119,7 +118,7 @@ func (builder *GradleBuilder) DiscoverModules(dir string) ([]config.ModuleConfig
taskListOutput, gradleTaskErr := exec.Command(builder.GradleCmd, "tasks", "--all", "-q", "-a", "--offline").Output()
if len(taskListOutput) > 0 && gradleTaskErr == nil {
// Search for subprojects using Gradle task list instead of grepping for build.gradle
var moduleConfigurations []config.ModuleConfig
var moduleConfigurations []module.Config
// NOTE: this leaves out the root ("") dependencies task. To include, replace with `(\w+:)?dependencies -`
taskListRe := regexp.MustCompile(`\w+:dependencies -`)
for _, line := range strings.Split(string(taskListOutput), "\n") {
Expand All @@ -129,7 +128,7 @@ func (builder *GradleBuilder) DiscoverModules(dir string) ([]config.ModuleConfig
if len(depMatchIndices) > 0 && depMatchIndices[0] == 0 {
gradleTask := strings.Split(trimmed, " - ")[0]
gradleLogger.Debugf("found gradle dependencies task: %s", gradleTask)
moduleConfigurations = append(moduleConfigurations, config.ModuleConfig{
moduleConfigurations = append(moduleConfigurations, module.Config{
Name: strings.Split(gradleTask, ":")[0] + ":compile", // Name is the gradle `task:configuration` (default to compile)
Path: "build.gradle",
Type: "gradle",
Expand All @@ -142,8 +141,8 @@ func (builder *GradleBuilder) DiscoverModules(dir string) ([]config.ModuleConfig
}

// Fall back to "app" as default task, even though technically it would be "" (root)
return []config.ModuleConfig{
config.ModuleConfig{
return []module.Config{
{
Name: "app:compile",
Path: "build.gradle",
Type: "gradle",
Expand Down
11 changes: 5 additions & 6 deletions builders/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/bmatcuk/doublestar"
logging "github.com/op/go-logging"

"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
)

Expand Down Expand Up @@ -154,7 +153,7 @@ func (builder *MavenBuilder) IsModule(target string) (bool, error) {
}

// DiscoverModules finds either a root pom.xml file or all pom.xmls in the specified dir
func (builder *MavenBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, error) {
func (builder *MavenBuilder) DiscoverModules(dir string) ([]module.Config, error) {
_, err := os.Stat(filepath.Join(dir, "pom.xml"))
if err == nil {
// Root pom found; parse and return
Expand All @@ -168,8 +167,8 @@ func (builder *MavenBuilder) DiscoverModules(dir string) ([]config.ModuleConfig,
}

}
return []config.ModuleConfig{
config.ModuleConfig{
return []module.Config{
{
Name: artifactName,
Path: "pom.xml",
Type: "mvn",
Expand All @@ -182,7 +181,7 @@ func (builder *MavenBuilder) DiscoverModules(dir string) ([]config.ModuleConfig,
if err != nil {
return nil, err
}
moduleConfigs := make([]config.ModuleConfig, len(pomFilePaths))
moduleConfigs := make([]module.Config, len(pomFilePaths))
for i, path := range pomFilePaths {
artifactName := filepath.Base(filepath.Dir(dir))
var artifactPom POMFile
Expand All @@ -194,7 +193,7 @@ func (builder *MavenBuilder) DiscoverModules(dir string) ([]config.ModuleConfig,
}
}
path, _ := filepath.Rel(dir, path)
moduleConfigs[i] = config.ModuleConfig{
moduleConfigs[i] = module.Config{
Name: artifactName,
Path: path,
Type: "mvn",
Expand Down
9 changes: 4 additions & 5 deletions builders/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/bmatcuk/doublestar"
logging "github.com/op/go-logging"

"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
)

Expand Down Expand Up @@ -162,8 +161,8 @@ func (builder *NodeJSBuilder) IsModule(target string) (bool, error) {
}

// DiscoverModules builds ModuleConfigs for any package.jsons that are not contained in a node_modules dir
func (builder *NodeJSBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, error) {
var moduleConfigs []config.ModuleConfig
func (builder *NodeJSBuilder) DiscoverModules(dir string) ([]module.Config, error) {
var moduleConfigs []module.Config
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
nodejsLogger.Debugf("Failed to access path %s: %s\n", path, err.Error())
Expand All @@ -186,10 +185,10 @@ func (builder *NodeJSBuilder) DiscoverModules(dir string) ([]config.ModuleConfig

nodejsLogger.Debugf("Found NodeJS package: %s (%s)", path, moduleName)
path, _ = filepath.Rel(dir, path)
moduleConfigs = append(moduleConfigs, config.ModuleConfig{
moduleConfigs = append(moduleConfigs, module.Config{
Name: moduleName,
Path: path,
Type: string(config.Nodejs),
Type: string(module.Nodejs),
})
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions builders/ruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/bmatcuk/doublestar"
logging "github.com/op/go-logging"

"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
)

Expand Down Expand Up @@ -153,12 +152,12 @@ func (builder *RubyBuilder) IsModule(target string) (bool, error) {
}

// DiscoverModules returns ModuleConfigs that match Gemfiles in the directory
func (builder *RubyBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, error) {
func (builder *RubyBuilder) DiscoverModules(dir string) ([]module.Config, error) {
gemFilePaths, err := doublestar.Glob(filepath.Join(dir, "**", "Gemfile"))
if err != nil {
return nil, err
}
moduleConfigs := make([]config.ModuleConfig, len(gemFilePaths))
moduleConfigs := make([]module.Config, len(gemFilePaths))
for i, path := range gemFilePaths {
gemName := filepath.Base(filepath.Dir(path))
// infer title from *.gemspec in directory if exists
Expand All @@ -176,7 +175,7 @@ func (builder *RubyBuilder) DiscoverModules(dir string) ([]config.ModuleConfig,
}
}
path, _ = filepath.Rel(dir, path)
moduleConfigs[i] = config.ModuleConfig{
moduleConfigs[i] = module.Config{
Name: gemName,
Path: path,
Type: "ruby",
Expand Down
11 changes: 5 additions & 6 deletions builders/sbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/bmatcuk/doublestar"
logging "github.com/op/go-logging"

"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
)

Expand Down Expand Up @@ -151,7 +150,7 @@ func (builder *SBTBuilder) IsModule(target string) (bool, error) {
}

// DiscoverModules returns a root build.sbt if found, and build configs for all sub-projects otherwise
func (builder *SBTBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, error) {
func (builder *SBTBuilder) DiscoverModules(dir string) ([]module.Config, error) {
_, err := os.Stat(filepath.Join(dir, "build.sbt"))
if err == nil {
// in a multi-project build (https://www.scala-sbt.org/1.x-beta/docs/Multi-Project.html) we return the root build.sbt only
Expand All @@ -160,8 +159,8 @@ func (builder *SBTBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, e
absDir = dir
}
artifactName := filepath.Base(absDir)
return []config.ModuleConfig{
config.ModuleConfig{
return []module.Config{
{
Name: artifactName,
Path: "build.sbt",
Type: "sbt",
Expand All @@ -174,10 +173,10 @@ func (builder *SBTBuilder) DiscoverModules(dir string) ([]config.ModuleConfig, e
if err != nil {
return nil, err
}
moduleConfigs := make([]config.ModuleConfig, len(sbtFilePaths))
moduleConfigs := make([]module.Config, len(sbtFilePaths))
for i, path := range sbtFilePaths {
artifactName := filepath.Dir(path) // Use the dirname as it's impossible to reliably parse from build.sbt
moduleConfigs[i] = config.ModuleConfig{
moduleConfigs[i] = module.Config{
Name: artifactName,
Path: path,
Type: "sbt",
Expand Down
5 changes: 2 additions & 3 deletions cmd/fossa/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"

"github.com/fossas/fossa-cli/builders"
"github.com/fossas/fossa-cli/config"
"github.com/fossas/fossa-cli/module"
logging "github.com/op/go-logging"
Expand Down Expand Up @@ -52,13 +51,13 @@ func analyzeCmd(c *cli.Context) {
}

type analysisKey struct {
builder builders.Builder
builder module.Builder
module module.Module
}

type analysis map[analysisKey][]module.Dependency

func doAnalyze(modules []config.ModuleConfig, allowUnresolved bool) (analysis, error) {
func doAnalyze(modules []module.Config, allowUnresolved bool) (analysis, error) {
analysisLogger.Debugf("Running analysis on modules: %#v", modules)
dependencies := make(analysis)

Expand Down

0 comments on commit c19b51b

Please sign in to comment.