Skip to content

Commit

Permalink
fix the bug for ignoring added errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed May 20, 2023
1 parent 421e1ce commit 5582ae5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 21 deletions.
49 changes: 35 additions & 14 deletions cmd/rrh/commands/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package add
import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
Expand All @@ -29,12 +30,21 @@ func New() *cobra.Command {
},
}
flags := cmd.Flags()
flags.StringSliceVarP(&addOpts.groups, "group", "g", []string{"no-group"}, "group for the repositories")
defaultGroup := findDefaultGroupName()
flags.StringSliceVarP(&addOpts.groups, "group", "g", []string{defaultGroup}, "group for the repositories")
flags.StringVarP(&addOpts.repoId, "repository-id", "r", "", "specifies the repository id. Specifying this option fails on multiple arguments")
flags.BoolVarP(&addOpts.dryRunFlag, "dry-run", "D", false, "dry-run mode")
return cmd
}

func findDefaultGroupName() string {
defaultGroupName := os.Getenv("RRH_DEFAULT_GROUP")
if defaultGroupName == "" {
defaultGroupName = "no-group"
}
return defaultGroupName
}

func validateArgs(c *cobra.Command, args []string) error {
repo, _ := c.Flags().GetString("repository-id")
if repo != "" && len(args) > 1 {
Expand All @@ -43,20 +53,18 @@ func validateArgs(c *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("too few arguments")
}
return validateGitDirs(args)
return nil
// return validateGitDirs(args)
}

func validateGitDirs(args []string) error {
messages := common.NewErrorList()
for _, arg := range args {
absPath, _ := filepath.Abs(arg)
err := rrh.IsExistAndGitRepository(absPath, arg)
messages.Append(err)
messages = messages.Append(err)
}
if messages.IsNil() {
return nil
}
return messages
return messages.NilOrThis()
}

func perform(c *cobra.Command, args []string, db *rrh.Database) error {
Expand All @@ -66,26 +74,36 @@ func perform(c *cobra.Command, args []string, db *rrh.Database) error {
}
for _, targetPath := range args {
err := addRepositoryToGroup(db, targetPath, addOpts.groups)
el.Append(err)
}
if el.IsErr() {
return el
el = el.Append(err)
}
if !addOpts.dryRunFlag {
db.StoreAndClose()
}
if el.IsErr() {
return el
}
return nil
}

func createGroups(db *rrh.Database, groups []string) common.ErrorList {
el := common.NewErrorList()
for _, groupName := range groups {
_, err := db.AutoCreateGroup(groupName, "", false)
el.Append(err)
el = el.Append(err)
}
el = el.Append(createDefaultGroupNameIfNeeded(db))
return el
}

func createDefaultGroupNameIfNeeded(db *rrh.Database) error {
defaultGroupName := findDefaultGroupName()
if db.HasGroup(defaultGroupName) {
return nil
}
_, err := db.CreateGroup(defaultGroupName, "default group", false)
return err
}

func findIDFromPath(repoIdFromOpts string, absPath string) string {
if repoIdFromOpts == "" {
return filepath.Base(absPath)
Expand All @@ -102,8 +120,11 @@ func isDuplicateRepositoryId(db *rrh.Database, repoId, path string) error {
}

func addRepositoryToGroup(db *rrh.Database, path string, groupNames []string) error {
var absPath, _ = filepath.Abs(path)
var id = findIDFromPath(addOpts.repoId, absPath)
absPath, _ := filepath.Abs(path)
if err := rrh.IsExistAndGitRepository(absPath, path); err != nil {
return err
}
id := findIDFromPath(addOpts.repoId, absPath)
if err1 := isDuplicateRepositoryId(db, id, absPath); err1 != nil {
return err1
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/rrh/commands/add/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func TestAdd(t *testing.T) {
[]repositoryChecker{{"fibonacci", true}},
[]relationChecker{{"no-group", "fibonacci", true}},
},
{[]string{"../../../../testdata/fibonacci", "../../../../testdata/helloworld", "../../../../not-exist-dir", "../../../../testdata/other/helloworld"}, false,
{[]string{"../../../../testdata/fibonacci", "../../../../testdata/helloworld", "../../../../not-exist-dir", "../../../../testdata/other/helloworld"}, true,
[]groupChecker{{"no-group", true}},
[]repositoryChecker{{"fibonacci", true}, {"helloworld", true}, {"not-exist-dir", false}},
[]relationChecker{{"no-group", "fibonacci", true}, {"no-group", "helloworld", true}},
},
{[]string{"../../../../testdata/helloworld", "../../../../testdata/other/helloworld"}, false,
{[]string{"../../../../testdata/helloworld", "../../../../testdata/other/helloworld"}, true,
[]groupChecker{},
[]repositoryChecker{{"helloworld", true}},
[]relationChecker{{"no-group", "helloworld", true}},
Expand Down
4 changes: 2 additions & 2 deletions cmd/rrh/commands/execcmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func validateRepos(repos []string, db *rrh.Database) ([]string, error) {
for _, r := range repos {
repo := db.FindRepository(r)
if repo == nil {
el.Append(fmt.Errorf("%s: repository not found", r))
el = el.Append(fmt.Errorf("%s: repository not found", r))
} else {
results = append(results, r)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func performExec(c *cobra.Command, args []string, db *rrh.Database) error {
el := common.NewErrorList()
for _, repository := range repositories {
repo := db.FindRepository(repository)
el.Append(execute(c, repo, args))
el = el.Append(execute(c, repo, args))
}
return el.NilOrThis()
}
2 changes: 1 addition & 1 deletion cmd/rrh/commands/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ExampleListCommand() {
// Output:
// no-group (2 repositories)
// rrh ~/go/src/github.com/tamada/rrh
// helloworld ../testdata/helloworld
// helloworld ../../../../testdata/helloworld
// 1 group, and 2 repositories
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/rrh/commands/repository/repository_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func performOf(c *cobra.Command, args []string, db *rrh.Database) error {
el := common.NewErrorList()
for _, arg := range args {
if !db.HasGroup(arg) {
el.Append(fmt.Errorf("%s: group not found", arg))
el = el.Append(fmt.Errorf("%s: group not found", arg))
}
}
if el.IsErr() {
Expand Down
2 changes: 1 addition & 1 deletion testdata/database.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository_path":"~/go/src/github.com/tamada/rrh"
},{
"repository_id":"helloworld",
"repository_path":"../testdata/helloworld"
"repository_path":"../../../../testdata/helloworld"
}],
"groups":[{
"group_name":"no-group",
Expand Down

0 comments on commit 5582ae5

Please sign in to comment.