Skip to content

Commit

Permalink
fix(config): refactor to fix locator flag setting
Browse files Browse the repository at this point in the history
  • Loading branch information
xizhao committed Jan 29, 2018
1 parent 668a4f9 commit 41c2d3e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
31 changes: 12 additions & 19 deletions cmd/fossa/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion cmd/fossa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -125,7 +126,7 @@ func BootstrapCmd(c *cli.Context) error {
}
}

// // CLI flags.
// CLI flags.
installFlag := c.Bool("install")
if installFlag {
config.CLI.Install = true
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 41c2d3e

Please sign in to comment.