Skip to content

Commit

Permalink
feat(cmd): Refactor CLI and config structure
Browse files Browse the repository at this point in the history
  • Loading branch information
elldritch committed Jan 26, 2018
1 parent 97626c5 commit 5161162
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 154 deletions.
2 changes: 1 addition & 1 deletion build/bower.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (ctx *BowerContext) Build(m *Module, opts map[string]interface{}) error {
}

// bower install
if ctx.verifyBowerComponents() == false || opts["no-cache"].(bool) == true {
if ctx.verifyBowerComponents() == false || opts["no_cache"].(bool) == true {
log.Logger.Debug("No prebuilt bower_components directory, building...")
exec.Command("bower", "install").Output()
} else {
Expand Down
2 changes: 1 addition & 1 deletion build/commonjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (ctx *CommonJSContext) Verify(m *Module, opts map[string]interface{}) bool

// Build determines and executes a CommonJS build based off available tooling in the environment
func (ctx *CommonJSContext) Build(m *Module, opts map[string]interface{}) error {
if ctx.verifyNodeModules() == false || opts["no-cache"].(bool) == true {
if ctx.verifyNodeModules() == false || opts["no_cache"].(bool) == true {
log.Logger.Debug("No prebuilt node_modules directory, building...")
if err := ctx.populateNodeModules(); err != nil {
return err
Expand Down
75 changes: 49 additions & 26 deletions cmd/fossa/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,38 @@ import (

// Config is a parsed configuration for the CLI and Worker.
type Config struct {
Cli struct {
CLI struct {
// Upload configuration.
APIKey string `yaml:"api_key"`
Server string
Project string
Locator string

// CLI flags.
Install bool
Upload bool
NoCache bool
LogLevel string
}
Analyze struct {
Ignores []struct {
Path string
Type string
}
Modules []struct {
Name string
Path string
Type string
}
Modules []ModuleConfig
}
}

// ModuleConfig is the configuration for a single module to be analysed.
type ModuleConfig struct {
Name string
Path string
Type string
}

// ReadConfig parses the configuration file in the current directory and sets
// default values if necessary.
func ReadConfig() (*Config, error) {
func ReadConfig() (Config, error) {
_, err := os.Stat(".fossa.yml")
if err == nil {
return parseConfig(".fossa.yml")
Expand All @@ -43,55 +53,68 @@ func ReadConfig() (*Config, error) {
return parseConfig(".fossa.yaml")
}

return nil, nil
return setDefaultValues(Config{})
}

func parseConfig(filename string) (*Config, error) {
func parseConfig(filename string) (Config, error) {
// Read configuration file.
var config Config

bytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
return config, err
}

err = yaml.Unmarshal(bytes, &config)
if err != nil {
return nil, err
return config, err
}

if len(config.Cli.Server) == 0 {
config.Cli.Server = "https://app.fossa.io"
config, err = setDefaultValues(config)
if err != nil {
return config, err
}

if len(config.Cli.Locator) == 0 {
return config, nil
}

func setDefaultValues(c Config) (Config, error) {
// Set default endpoint.
if len(c.CLI.Server) == 0 {
c.CLI.Server = "https://app.fossa.io"
}

// Load API key from environment variable.
if len(c.CLI.APIKey) == 0 {
c.CLI.APIKey = os.Getenv("FOSSA_CLI_API_KEY")
}

// Infer default locator and project from `git`.
if len(c.CLI.Locator) == 0 {
repo, err := git.PlainOpen(".")
if err != nil {
return nil, err
return c, err
}

project := config.Cli.Project
project := c.CLI.Project
if len(project) == 0 {
origin, err := repo.Remote("origin")
if err != nil {
return nil, err
return c, err
}
if origin == nil {
return nil, errors.New("could not infer project name from either `.fossa.yaml` or `git` remote named `origin`")
return c, errors.New("could not infer project name from either `.fossa.yaml` or `git` remote named `origin`")
}
project = origin.Config().URLs[0]
config.Cli.Project = project
c.CLI.Project = project
}

revision, err := repo.Head()
if err != nil {
return nil, err
return c, err
}
config.Cli.Locator = project + "$" + revision.Hash().String()
}

if len(config.Cli.APIKey) == 0 {
config.Cli.APIKey = os.Getenv("FOSSA_CLI_API_KEY")
c.CLI.Locator = project + "$" + revision.Hash().String()
}

return &config, nil
return c, nil
}

0 comments on commit 5161162

Please sign in to comment.