From 41c2d3e4cab99be8c46c3cae7fd93cc660088ef8 Mon Sep 17 00:00:00 2001 From: xizhao Date: Sun, 28 Jan 2018 21:13:02 -0800 Subject: [PATCH] fix(config): refactor to fix locator flag setting --- cmd/fossa/config.go | 31 ++++++++++++------------------- cmd/fossa/main.go | 11 ++++++++++- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/cmd/fossa/config.go b/cmd/fossa/config.go index a9913c18f7..2e374e7a61 100644 --- a/cmd/fossa/config.go +++ b/cmd/fossa/config.go @@ -107,28 +107,21 @@ func setDefaultValues(c Config) (Config, error) { // Infer default locator and project from `git`. if len(c.CLI.Locator) == 0 { repo, err := git.PlainOpen(".") - if err != nil { - return c, errors.New("no revision found in working directory; try running in a git repo or passing a locator") - } - - project := c.CLI.Project - if len(project) == 0 { - origin, err := repo.Remote("origin") - if err != nil { - return c, err - } - if origin == nil { - return c, errors.New("could not infer project name from either `.fossa.yaml` or `git` remote named `origin`") + if err == nil { + project := c.CLI.Project + if len(project) == 0 { + origin, err := repo.Remote("origin") + if err == nil && origin != nil { + project = origin.Config().URLs[0] + c.CLI.Project = project + } } - project = origin.Config().URLs[0] - c.CLI.Project = project - } - revision, err := repo.Head() - if err != nil { - return c, err + revision, err := repo.Head() + if err == nil { + c.CLI.Locator = "git+" + project + "$" + revision.Hash().String() + } } - c.CLI.Locator = "git+" + project + "$" + revision.Hash().String() } return c, nil diff --git a/cmd/fossa/main.go b/cmd/fossa/main.go index 97f924ec0a..47a642e38d 100644 --- a/cmd/fossa/main.go +++ b/cmd/fossa/main.go @@ -51,6 +51,7 @@ func main() { Action: BuildCmd, Flags: []cli.Flag{ // Format: `type:path` e.g. `gopackage:github.com/fossas/fossa-cli/cmd/fossa` + cli.StringFlag{Name: "locator"}, cli.StringFlag{Name: "entry_point, e"}, cli.StringFlag{Name: "type, t"}, cli.BoolFlag{Name: "install, i", Usage: "run a default build in module directories if they have not been pre-built"}, @@ -125,7 +126,7 @@ func BootstrapCmd(c *cli.Context) error { } } - // // CLI flags. + // CLI flags. installFlag := c.Bool("install") if installFlag { config.CLI.Install = true @@ -157,6 +158,14 @@ func BootstrapCmd(c *cli.Context) error { config.CLI.APIKey = apiKeyFlag } + if config.CLI.Locator == "" { + log.Logger.Fatal("no revision found in working directory; try running in a git repo or passing a locator") + } + + if config.CLI.Project == "" { + log.Logger.Fatal("could not infer project name from either `.fossa.yaml` or `git` remote named `origin`") + } + context.config = config return nil }