Skip to content

Commit

Permalink
added custom-project flag
Browse files Browse the repository at this point in the history
  • Loading branch information
anuccio1 committed Mar 1, 2018
1 parent 5c5bdcd commit d7fbaf2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
53 changes: 25 additions & 28 deletions cmd/fossa/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ type configFileV1 struct {

CLI struct {
// Upload configuration.
APIKey string `yaml:"api_key"`
Server string
Fetcher string // for now we don't allow users to set this
Project string
APIKey string `yaml:"api_key"`
Server string
Project string
Revision string
}
Analyze struct {
Modules []moduleConfig
Expand Down Expand Up @@ -99,12 +99,8 @@ func setDefaultValues(c configFileV1) (configFileV1, error) {
c.CLI.APIKey = os.Getenv("FOSSA_API_KEY")
}

// default to `Custom` fetcher type
// if they run CLI inside a git repo and haven't given a project name in the config, we change fetcher to git
var fetcher = "custom"

// Infer project and revision from `git`.
if c.CLI.Project == "" || c.CLI.Revision == "" {
if c.CLI.Project == "" {
// TODO: this needs to happen in the module directory, not the working directory
repo, err := git.PlainOpen(".")
if err == nil {
Expand All @@ -114,7 +110,6 @@ func setDefaultValues(c configFileV1) (configFileV1, error) {
if err == nil && origin != nil {
project = origin.Config().URLs[0]
c.CLI.Project = project
fetcher = "git" // project is a git remote, so fetcher must be as well
}
}
revision := c.CLI.Revision
Expand All @@ -127,19 +122,18 @@ func setDefaultValues(c configFileV1) (configFileV1, error) {
}
}

c.CLI.Fetcher = fetcher // set fetcher in config

return c, nil
}

type cliConfig struct {
apiKey string
fetcher string
project string
revision string
endpoint string
modules []moduleConfig
debug bool
apiKey string
fetcher string
project string
revision string
endpoint string
modules []moduleConfig
debug bool
customProject bool

defaultConfig defaultConfig
analyzeConfig analyzeConfig
Expand Down Expand Up @@ -188,12 +182,13 @@ func parseModuleFlag(moduleFlag string) []moduleConfig {

func initialize(c *cli.Context) (cliConfig, error) {
var config = cliConfig{
apiKey: c.String("api_key"),
project: c.String("project"),
revision: c.String("revision"),
endpoint: c.String("endpoint"),
modules: parseModuleFlag(c.String("modules")),
debug: c.Bool("debug"),
apiKey: c.String("api_key"),
project: c.String("project"),
revision: c.String("revision"),
endpoint: c.String("endpoint"),
modules: parseModuleFlag(c.String("modules")),
debug: c.Bool("debug"),
customProject: c.Bool("custom-project"),

defaultConfig: defaultConfig{
build: c.Bool("build"),
Expand Down Expand Up @@ -224,9 +219,6 @@ func initialize(c *cli.Context) (cliConfig, error) {
return cliConfig{}, err
}

if config.fetcher == "" {
config.fetcher = configFile.CLI.Fetcher
}
if config.project == "" {
config.project = configFile.CLI.Project
}
Expand All @@ -243,6 +235,11 @@ func initialize(c *cli.Context) (cliConfig, error) {
config.modules = configFile.Analyze.Modules
}

config.fetcher = "git"
if config.customProject {
config.fetcher = "custom"
}

// Configure logging.
if config.debug {
formatter := logging.MustStringFormatter(`%{color}%{time} %{level} %{module}:%{shortpkg}/%{shortfile}/%{shortfunc}%{color:reset} %{message}`)
Expand Down
3 changes: 3 additions & 0 deletions cmd/fossa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
analyzeOutputUsage = "print results to stdout instead of uploading to FOSSA"
analyzeAllowResolvedUsage = "allow unresolved dependencies"
debugUsage = "print debug information to stderr"
managedBuildUsage = "upload results as a custom project to FOSSA"
)

func main() {
Expand All @@ -51,6 +52,7 @@ func main() {
cli.BoolFlag{Name: "b, build", Usage: "run a default build in module directories if they have not been pre-built"},
cli.BoolFlag{Name: "f, force", Usage: buildForceUsage},
cli.BoolFlag{Name: "debug", Usage: debugUsage},
cli.BoolFlag{Name: "custom-project", Usage: managedBuildUsage},
}

app.Commands = []cli.Command{
Expand Down Expand Up @@ -89,6 +91,7 @@ func main() {
cli.BoolFlag{Name: "o, output", Usage: analyzeOutputUsage},
cli.BoolFlag{Name: "allow-unresolved", Usage: analyzeAllowResolvedUsage},
cli.BoolFlag{Name: "debug", Usage: debugUsage},
cli.BoolFlag{Name: "custom-project", Usage: managedBuildUsage},
},
},
{
Expand Down
9 changes: 7 additions & 2 deletions cmd/fossa/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ func doUpload(conf config.CLIConfig, results []normalizedModule) (string, error)

analysisLogger.Debugf("Uploading build data from (%#v) modules: %#v", len(results), string(buildData))

postRef, _ := url.Parse("/api/builds/custom?locator=" + url.QueryEscape(config.MakeLocator(conf.fetcher, conf.project, conf.revision)) + "&v=" + version)
fossaEndpoint := "/api/builds/custom?locator=" + url.QueryEscape(config.MakeLocator(conf.fetcher, conf.project, conf.revision)) + "&v=" + version
if config.customProject {
fossaEndpoint += "&managedBuild=true"
}

postRef, _ := url.Parse(fossaEndpoint)
postURL := fossaBaseURL.ResolveReference(postRef).String()

analysisLogger.Debugf("Sending build data to <%#v>", postURL)
Expand All @@ -156,7 +161,7 @@ func doUpload(conf config.CLIConfig, results []normalizedModule) (string, error)
return "", fmt.Errorf("invalid API key (check the $FOSSA_API_KEY environment variable)")
} else if resp.StatusCode == http.StatusPreconditionRequired {
// TODO: handle "Managed Project" workflow
return "", fmt.Errorf("invalid project or revision; make sure this version is published and FOSSA has access to your repo")
return "", fmt.Errorf("invalid project or revision; make sure this version is published and FOSSA has access to your repo. To submit a custom project, add the --custom-project flag")
} else if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("bad server response (%#v)", responseStr)
}
Expand Down

0 comments on commit d7fbaf2

Please sign in to comment.