Skip to content

Commit

Permalink
ref #13 introduce sort flag and update for sorting the database entri…
Browse files Browse the repository at this point in the history
…es on updating.
  • Loading branch information
tamada committed Mar 24, 2019
1 parent 44bd21c commit 03e02c2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
16 changes: 8 additions & 8 deletions clone/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func TestCloneCommand_MultipleProjects(t *testing.T) {
if len(db.Repositories) != 4 {
t.Fatal("helloworld and fibonacci were not registered.")
}
var hwRepo = db.Repositories[1]
if message := validate(hwRepo, "helloworld", "../testdata/hoge/helloworld"); message != "" {
var hwRepo = db.FindRepository("helloworld")
if message := validate(*hwRepo, "helloworld", "../testdata/hoge/helloworld"); message != "" {
t.Error(message)
}
var fiboRepo = db.Repositories[0]
if message := validate(fiboRepo, "fibonacci", "../testdata/hoge/fibonacci"); message != "" {
var fiboRepo = db.FindRepository("fibonacci")
if message := validate(*fiboRepo, "fibonacci", "../testdata/hoge/fibonacci"); message != "" {
t.Error(message)
}
if !db.HasGroup("not-exist-group") || len(db.Groups) != 3 {
Expand All @@ -92,8 +92,8 @@ func TestCloneCommand_Run(t *testing.T) {
if len(db.Repositories) != 3 {
t.Fatal("helloworld was not registered.")
}
var repo = db.Repositories[0]
if message := validate(repo, "helloworld", "./helloworld"); message != "" {
var repo = db.FindRepository("helloworld")
if message := validate(*repo, "helloworld", "./helloworld"); message != "" {
t.Error(message)
}
if db.ContainsCount("no-group") != 1 || !db.HasRelation("no-group", "helloworld") {
Expand All @@ -115,8 +115,8 @@ func TestCloneCommand_SpecifyingId(t *testing.T) {
if len(db.Repositories) != 3 {
t.Fatal("newid was not registered.")
}
var repo = db.Repositories[0]
if message := validate(repo, "newid", "../testdata/newid"); message != "" {
var repo = db.FindRepository("newid")
if message := validate(*repo, "newid", "../testdata/newid"); message != "" {
t.Error(message)
}
})
Expand Down
12 changes: 12 additions & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
RrhAutoDeleteGroup = "RRH_AUTO_DELETE_GROUP"
RrhAutoCreateGroup = "RRH_AUTO_CREATE_GROUP"
RrhTimeFormat = "RRH_TIME_FORMAT"
RrhSortOnUpdating = "RRH_SORT_ON_UPDATING"
)

const (
Expand All @@ -45,6 +46,7 @@ type Config struct {
DefaultGroupName string `json:"rrh_default_group_name"`
TimeFormat string `json:"rrh_time_format"`
OnError string `json:"rrh_on_error"`
SortOnUpdating string `json:"rrh_sort_on_updating"`
}

func trueOrFalse(value string) (string, error) {
Expand Down Expand Up @@ -78,6 +80,12 @@ func (config *Config) Update(label string, value string) error {
config.AutoCreateGroup = flag
}
return err
case RrhSortOnUpdating:
var flag, err = trueOrFalse(value)
if err == nil {
config.SortOnUpdating = flag
}
return err
case RrhHome:
config.Home = value
return nil
Expand Down Expand Up @@ -118,6 +126,8 @@ func (config *Config) GetString(label string) (value string, readFrom string) {
return config.getStringFromEnv(RrhAutoDeleteGroup, config.AutoDeleteGroup)
case RrhAutoCreateGroup:
return config.getStringFromEnv(RrhAutoCreateGroup, config.AutoCreateGroup)
case RrhSortOnUpdating:
return config.getStringFromEnv(RrhSortOnUpdating, config.SortOnUpdating)
case RrhHome:
return config.getStringFromEnv(RrhHome, config.Home)
case RrhConfigPath:
Expand Down Expand Up @@ -164,6 +174,8 @@ func (config *Config) findDefaultValue(label string) (value string, readFrom str
return "false", Default
case RrhAutoCreateGroup:
return "false", Default
case RrhSortOnUpdating:
return "false", Default
default:
return "", NotFound
}
Expand Down
1 change: 1 addition & 0 deletions common/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (clc *configListCommand) Run(args []string) int {
fmt.Println(config.formatVariableAndValue(RrhTimeFormat))
fmt.Println(config.formatVariableAndValue(RrhAutoCreateGroup))
fmt.Println(config.formatVariableAndValue(RrhAutoDeleteGroup))
fmt.Println(config.formatVariableAndValue(RrhSortOnUpdating))
return 0
}

Expand Down
2 changes: 2 additions & 0 deletions common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func ExampleConfigCommand_Run() {
// RRH_TIME_FORMAT: relative (default)
// RRH_AUTO_CREATE_GROUP: true (config_file)
// RRH_AUTO_DELETE_GROUP: false (config_file)
// RRH_SORT_ON_UPDATING: true (config_file)
}
func Example_configListCommand_Run() {
os.Setenv(RrhConfigPath, "../testdata/config.json")
Expand All @@ -107,6 +108,7 @@ func Example_configListCommand_Run() {
// RRH_TIME_FORMAT: relative (default)
// RRH_AUTO_CREATE_GROUP: true (config_file)
// RRH_AUTO_DELETE_GROUP: false (config_file)
// RRH_SORT_ON_UPDATING: true (config_file)
}

func TestOpenConfigBrokenJson(t *testing.T) {
Expand Down
40 changes: 24 additions & 16 deletions common/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ func (db *Database) FindGroup(groupID string) *Group {
return nil
}

func sortIfNeeded(db *Database) {
if db.Config.GetValue(RrhSortOnUpdating) == "yes" {
sort.Slice(db.Repositories, func(i, j int) bool {
return db.Repositories[i].ID < db.Repositories[j].ID
})
sort.Slice(db.Groups, func(i, j int) bool {
return db.Groups[i].Name < db.Groups[j].Name
})
sort.Slice(db.Relations, func(i, j int) bool {
if db.Relations[i].GroupName == db.Relations[j].GroupName {
return db.Relations[i].RepositoryID < db.Relations[j].RepositoryID
}
return db.Relations[i].GroupName < db.Relations[j].GroupName
})
}
}

/*
CreateRepository returns the repository by creating the given parameters and store it to database.
*/
Expand All @@ -146,9 +163,7 @@ func (db *Database) CreateRepository(repoID string, path string, remotes []Remot
}
var repo = Repository{repoID, path, remotes}
db.Repositories = append(db.Repositories, repo)
sort.Slice(db.Repositories, func(i, j int) bool {
return db.Repositories[i].ID < db.Repositories[j].ID
})
sortIfNeeded(db)

return &repo, nil
}
Expand All @@ -162,10 +177,7 @@ func (db *Database) CreateGroup(groupID string, description string) (*Group, err
}
var group = Group{groupID, description}
db.Groups = append(db.Groups, group)

sort.Slice(db.Groups, func(i, j int) bool {
return db.Groups[i].Name < db.Groups[j].Name
})
sortIfNeeded(db)

return &group, nil
}
Expand All @@ -184,9 +196,7 @@ func (db *Database) UpdateGroup(groupID string, newGroupID string, newDescriptio
db.Groups[i].Description = newDescription
}
}
sort.Slice(db.Groups, func(i, j int) bool {
return db.Groups[i].Name < db.Groups[j].Name
})
sortIfNeeded(db)

return true
}
Expand All @@ -201,9 +211,7 @@ func (db *Database) Relate(groupID string, repoID string) error {
return nil
}
db.Relations = append(db.Relations, Relation{repoID, groupID})
sort.Slice(db.Relations, func(i, j int) bool {
return db.Relations[i].GroupName < db.Relations[j].GroupName
})
sortIfNeeded(db)

return nil
}
Expand Down Expand Up @@ -329,7 +337,7 @@ func (db *Database) DeleteRepository(repoID string) error {
return nil
}

func (db *Database) deleteGroup(groupID string) error {
func deleteGroup(db *Database, groupID string) error {
var groups = []Group{}
for _, group := range db.Groups {
if group.Name != groupID {
Expand All @@ -353,7 +361,7 @@ func (db *Database) DeleteGroup(groupID string) error {
if groups[groupID] != 0 {
return fmt.Errorf("%s: group has %d relatins", groupID, groups[groupID])
}
return db.deleteGroup(groupID)
return deleteGroup(db, groupID)
}

/*
Expand All @@ -365,7 +373,7 @@ func (db *Database) ForceDeleteGroup(groupID string) error {
return fmt.Errorf("%s: group not found", groupID)
}
db.UnrelateFromGroup(groupID)
return db.deleteGroup(groupID)
return deleteGroup(db, groupID)
}

func databasePath(config *Config) string {
Expand Down
6 changes: 3 additions & 3 deletions remove/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func TestRemoveRepository(t *testing.T) {
if err := rm.executeRemoveRepository(db, "unknown-repo"); err == nil {
t.Error("unknown-repo: found")
}
var _ = rm.executeRemoveRepository(db, "repo1")
if len(db.Repositories) != 1 {
t.Error("repo1 did not remove?")
var err = rm.executeRemoveRepository(db, "repo1")
if err != nil || len(db.Repositories) != 1 {
t.Errorf("repo1 did not remove?: %s", err.Error())
}
if len(db.Groups) != 2 {
t.Error("the number of groups changed")
Expand Down

0 comments on commit 03e02c2

Please sign in to comment.