-
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.
- Loading branch information
Showing
68 changed files
with
1,250 additions
and
936 deletions.
There are no files selected for viewing
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,166 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/fossas/fossa-cli/module" | ||
git "gopkg.in/src-d/go-git.v4" | ||
yaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
type configFileV1 struct { | ||
Version int `yaml:"version"` | ||
|
||
CLI configFileCLIV1 | ||
Analyze configFileAnalyzeV1 | ||
} | ||
|
||
type configFileCLIV1 struct { | ||
// Upload configuration. | ||
APIKey string `yaml:"api_key,omitempty"` | ||
Server string `yaml:"server,omitempty"` | ||
Fetcher string `yaml:"fetcher,omitempty"` // Defaults to custom | ||
Project string `yaml:"project,omitempty"` | ||
Revision string `yaml:"revision,omitempty"` | ||
Branch string `yaml:"branch,omitempty"` // Only used with custom fetcher | ||
} | ||
|
||
type configFileAnalyzeV1 struct { | ||
Modules []module.Config `yaml:"modules,omitempty"` | ||
} | ||
|
||
func readConfigFile(path string) (string, configFileV1, error) { | ||
if _, err := os.Stat(path); path != "" && err != nil && os.IsNotExist(err) { | ||
return path, configFileV1{}, fmt.Errorf("invalid config file specified") | ||
} else if _, err := os.Stat(".fossa.yml"); err == nil { | ||
path = ".fossa.yml" | ||
} else if _, err = os.Stat(".fossa.yaml"); err == nil { | ||
path = ".fossa.yaml" | ||
} | ||
|
||
if path == "" { | ||
conf, err := setDefaultValues(configFileV1{}) | ||
return path, conf, err | ||
} | ||
conf, err := parseConfigFile(path) | ||
return path, conf, err | ||
} | ||
|
||
func parseConfigFile(filename string) (configFileV1, error) { | ||
// Read configuration file. | ||
var config configFileV1 | ||
|
||
bytes, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
err = yaml.Unmarshal(bytes, &config) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
config, err = setDefaultValues(config) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
func setDefaultValues(c configFileV1) (configFileV1, error) { | ||
// Set config version | ||
c.Version = 1 | ||
|
||
// Set default endpoint. | ||
endpoint := os.Getenv("FOSSA_ENDPOINT") | ||
if endpoint != "" { | ||
c.CLI.Server = endpoint | ||
} | ||
if c.CLI.Server == "" { | ||
c.CLI.Server = "https://app.fossa.io" | ||
} | ||
|
||
// Load API key from environment variable. | ||
apiKey := os.Getenv("FOSSA_API_KEY") | ||
if apiKey != "" { | ||
c.CLI.APIKey = apiKey | ||
} | ||
|
||
// Default to custom. | ||
if c.CLI.Fetcher == "" { | ||
c.CLI.Fetcher = "custom" | ||
} | ||
|
||
// Infer default locator and project from `git`. | ||
if c.CLI.Project == "" || c.CLI.Revision == "" || c.CLI.Branch == "" { | ||
// TODO: this needs to happen in the module directory, not the working | ||
// directory | ||
repo, err := git.PlainOpen(".") | ||
if err == nil { | ||
if c.CLI.Project == "" { | ||
origin, err := repo.Remote("origin") | ||
if err == nil && origin != nil { | ||
c.CLI.Project = origin.Config().URLs[0] | ||
} | ||
} | ||
if c.CLI.Revision == "" { | ||
revision, err := repo.Head() | ||
if err == nil { | ||
c.CLI.Revision = revision.Hash().String() | ||
} | ||
} | ||
if c.CLI.Branch == "" { | ||
revision, err := repo.Head() | ||
if err == nil { | ||
c.CLI.Revision = revision.Hash().String() | ||
} | ||
c.CLI.Branch = revision.Name().String() | ||
} | ||
} | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
// WriteConfigFile writes a config state to yaml | ||
func WriteConfigFile(conf *CLIConfig) error { | ||
if conf.ConfigFilePath == "" { | ||
conf.ConfigFilePath = ".fossa.yml" | ||
} | ||
|
||
keyToWrite := "" | ||
if os.Getenv("FOSSA_API_KEY") == "" { | ||
keyToWrite = conf.APIKey | ||
} | ||
|
||
writeConfig := configFileV1{ | ||
Version: 1, | ||
CLI: configFileCLIV1{ | ||
APIKey: keyToWrite, | ||
Server: conf.Endpoint, | ||
Project: conf.Project, | ||
Fetcher: conf.Fetcher, | ||
}, | ||
Analyze: configFileAnalyzeV1{ | ||
Modules: conf.Modules, | ||
}, | ||
} | ||
yamlConfig, err := yaml.Marshal(writeConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
configHeader := []byte( | ||
strings.Join([]string{ | ||
"# Generated by FOSSA CLI (https://github.com/fossas/fossa-cli)", | ||
"# Visit https://fossa.io to learn more", | ||
"", | ||
"", | ||
}, "\n")) | ||
|
||
return ioutil.WriteFile(conf.ConfigFilePath, append(configHeader, yamlConfig...), 0777) | ||
} |
File renamed without changes.
File renamed without changes.
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,12 +1,13 @@ | ||
# Generated by FOSSA CLI (https://github.com/fossas/fossa-cli) | ||
# Visit https://fossa.io to learn more | ||
|
||
version: 1 | ||
cli: | ||
server: https://app.fossa.io | ||
project: git@github.com:fossas/fossa-cli.git | ||
fetcher: git | ||
project: git@github.com:fossas/fossa-cli.git | ||
analyze: | ||
modules: | ||
- name: fossa | ||
path: cmd/fossa | ||
type: go | ||
type: Go |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,105 @@ | ||
package module | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// A Module is a unit of buildable code within a project. | ||
type Module struct { | ||
Name string | ||
Type Type | ||
|
||
// Target is the entry point or manifest path for the build system. The exact | ||
// meaning is Type-dependent. | ||
Target string | ||
// Dir is the absolute path to the module's working directory (the directory | ||
// you would normally run the build command from). | ||
Dir string | ||
|
||
// Different modules in a monolith may correspond to different FOSSA projects | ||
// or revisions. | ||
// TODO: use these values. | ||
Project string | ||
Revision string | ||
|
||
// A catch-all for builders to add metadata (a la Context.Value). | ||
Context interface{} | ||
} | ||
|
||
// New instantiates and sets up a Module for a given ModuleType | ||
func New(moduleType Type, conf Config) (Module, error) { | ||
var manifestName string | ||
var moduleTarget string | ||
|
||
// Find root dir of module | ||
modulePath, err := filepath.Abs(conf.Path) | ||
if err != nil { | ||
return Module{}, err | ||
} | ||
|
||
// infer default module settings from type | ||
switch moduleType { | ||
case Ant: | ||
manifestName = "build.xml" | ||
break | ||
case Bower: | ||
manifestName = "bower.json" | ||
break | ||
case Cocoapods: | ||
manifestName = "Podfile" | ||
break | ||
case Composer: | ||
manifestName = "composer.json" | ||
break | ||
case Golang: | ||
manifestName = "" | ||
moduleTarget = strings.TrimPrefix(modulePath, filepath.Join(os.Getenv("GOPATH"), "src")+"/") | ||
break | ||
case Maven: | ||
manifestName = "pom.xml" | ||
break | ||
case Nodejs: | ||
manifestName = "package.json" | ||
break | ||
case NuGet: | ||
moduleTarget = modulePath | ||
modulePath = filepath.Dir(modulePath) | ||
break | ||
case Pip: | ||
manifestName = "requirements.txt" | ||
break | ||
case Ruby: | ||
manifestName = "Gemfile" | ||
break | ||
case SBT: | ||
manifestName = "build.sbt" | ||
break | ||
case VendoredArchives: | ||
manifestName = "" | ||
break | ||
} | ||
|
||
// trim manifest from path | ||
if filepath.Base(modulePath) == manifestName { | ||
modulePath = filepath.Dir(modulePath) | ||
} | ||
|
||
moduleName := conf.Name | ||
if moduleName == "" { | ||
moduleName = conf.Path | ||
} | ||
|
||
if moduleTarget == "" { | ||
moduleTarget = filepath.Join(modulePath, manifestName) | ||
} | ||
|
||
return Module{ | ||
Name: moduleName, | ||
Type: moduleType, | ||
Target: moduleTarget, | ||
Dir: modulePath, | ||
Context: conf.Options, | ||
}, nil | ||
} |
File renamed without changes.
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,9 +1,8 @@ | ||
{ | ||
"files.watcherExclude": { | ||
"**/.git/objects/**": true, | ||
"**/.git/subtree-cache/**": true, | ||
"**/node_modules/**": true, | ||
"text/fixtures/*/**": true, | ||
"**/.git/**": true, | ||
"**/.git/": true, | ||
"testdata/fixtures/**": true, | ||
"vendor/**": true | ||
} | ||
} | ||
} |
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.