Skip to content

Commit

Permalink
Merge pull request #12 from tamada/issue/update_database_layouts
Browse files Browse the repository at this point in the history
Issue/update database layouts
  • Loading branch information
tamada committed Mar 23, 2019
2 parents f84a5af + 3be6554 commit 9b83970
Show file tree
Hide file tree
Showing 27 changed files with 468 additions and 173 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ go:
env:
- secure: "Kt1ybTvi0s8BtV3FOgwAuiQ7pLTQQ9+Vuj7D0tJijRpzLl0W9vZXH9qzis2VHBSktOsK+CVxkawWyT+/uBMopzrydMQj0csL5tYteqcvUkX3jGMPZ0ygJf6SEHRST53Ej27F0Mr0pykYHimkhoQQ9WlvUGXDtubuUkFJ41n2fnCRjfUGpuIkIuNiuVxrMzV4xIo1nWli5VTIvybl+pplHQ1Grbs7tcKn8ok1c1TMoDqaV85l/Cl+GPLV4coR6Nd/3hNv/w9/CkGS9bqYa/imAIInkzEeHXU/KvT97xkAzUSRSYhnxWMx2G7MC6hOfWr75xcBxWpIDdss5qKXIIrqcUfALmMSzl1byPJWouRVaROVBMHI4fUQ9diKSTmZlmCzau1nQ2Zw0DXce4cowwlilBKwW0FDOLrsOevLNtCZ9cjLnhngxbHyg18eSHcCcs3xfPnw82Y/5TgljMl+rtdhO63D+PoHtOAJcwAX/lSVD5JpHir5WX75ynDW9+VXRnfhGIe3F5LndsS+N0Ar6OI3NEfLy0MqWB/Zts4lP3K5mkl9h3aghEFbdTjs8A2Vs3Gbi1Hs3RhCBiovG+wOvXB1zTlUNH7mRTU5Z9nnGsgYA6wH+yrwSKhZLVrXXmFm+i0X6ew4oO5n4ahJvfVWCaaP8oiRzrPQz/rNSXRxKcX1RS4="

before_script:
- export TZ=Asia/Tokyo

script:
- make update test
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
15 changes: 9 additions & 6 deletions add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,23 @@ func (add *AddCommand) addRepositoryToGroup(db *common.Database, groupName strin
var repoPath = common.NormalizePath(absPath)
if err1 := checkDuplication(db, id, absPath); err1 != nil {
return append(list, err1)
} else {
var remotes, err2 = FindRemotes(absPath)
if err2 != nil {
return append(list, err2)
}
db.CreateRepository(id, repoPath, remotes)
}
var remotes, err2 = FindRemotes(absPath)
if err2 != nil {
return append(list, err2)
}
db.CreateRepository(id, repoPath, remotes)

var err = db.Relate(groupName, id)
if err != nil {
return append(list, fmt.Errorf("%s: cannot create relation to group %s", id, groupName))
}
return list
}

/*
AddRepositoriesToGroup registers the given repositories to the specified group.
*/
func (add *AddCommand) AddRepositoriesToGroup(db *common.Database, args []string, groupName string) []error {
var err = add.createGroupIfNeeded(db, groupName)
if err != nil {
Expand Down
26 changes: 18 additions & 8 deletions clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"github.com/tamada/rrh/common"
)

func (clone *CloneCommand) toDir(db *common.Database, url string, dest string, repoID string) (*common.Repository, error) {
clone.printIfVerbose(fmt.Sprintf("git clone %s %s (%s)", url, dest, repoID))
var cmd = exec.Command("git", "clone", url, dest)
func (clone *CloneCommand) toDir(db *common.Database, URL string, dest string, repoID string) (*common.Repository, error) {
clone.printIfVerbose(fmt.Sprintf("git clone %s %s (%s)", URL, dest, repoID))
var cmd = exec.Command("git", "clone", URL, dest)
var err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("%s: clone error (%s)", url, err.Error())
return nil, fmt.Errorf("%s: clone error (%s)", URL, err.Error())
}

path, err := filepath.Abs(dest)
Expand Down Expand Up @@ -45,6 +45,9 @@ func (clone *CloneCommand) isExistDir(path string) bool {
return !os.IsNotExist(err) && stat.IsDir()
}

/*
DoClone performs `git clone` command and register the cloned repositories to RRH database.
*/
func (clone *CloneCommand) DoClone(db *common.Database, arguments []string) (int, []error) {
if len(arguments) == 1 {
var err = clone.DoCloneARepository(db, arguments[0])
Expand All @@ -56,7 +59,7 @@ func (clone *CloneCommand) DoClone(db *common.Database, arguments []string) (int
var errorlist = []error{}
var count = 0
for _, url := range arguments {
var increment, err = clone.DoCloneRepositories(db, url)
var increment, err = clone.DoCloneEachRepository(db, url)
if err != nil {
errorlist = append(errorlist, err)
if db.Config.GetValue(common.RrhOnError) == common.FailImmediately {
Expand All @@ -80,11 +83,15 @@ func (clone *CloneCommand) relateTo(db *common.Database, groupID string, repoID
return nil
}

func (clone *CloneCommand) DoCloneRepositories(db *common.Database, url string) (int, error) {
/*
DoCloneEachRepository performes `git clone` for each repository.
This function is called repeatedly.
*/
func (clone *CloneCommand) DoCloneEachRepository(db *common.Database, URL string) (int, error) {
var count int
var id = findID(url)
var id = findID(URL)
var path = filepath.Join(clone.Options.dest, id)
var _, err = clone.toDir(db, url, path, id)
var _, err = clone.toDir(db, URL, path, id)
if err == nil {
if err := clone.relateTo(db, clone.Options.group, id); err != nil {
return count, err
Expand All @@ -94,6 +101,9 @@ func (clone *CloneCommand) DoCloneRepositories(db *common.Database, url string)
return count, err
}

/*
DoCloneARepository clones a repository from given URL.
*/
func (clone *CloneCommand) DoCloneARepository(db *common.Database, URL string) error {
var id, path string

Expand Down
9 changes: 9 additions & 0 deletions clone/clone_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import (
"github.com/tamada/rrh/common"
)

/*
CloneCommand represents a command.
*/
type CloneCommand struct {
Options *cloneOptions
}

/*
CloneCommandFactory returns an instance of the CloneCommand.
*/
func CloneCommandFactory() (cli.Command, error) {
return &CloneCommand{&cloneOptions{}}, nil
}
Expand Down Expand Up @@ -54,6 +60,9 @@ func (options *cloneOptions) showError(list []error) {
}
}

/*
Run performs the command.
*/
func (clone *CloneCommand) Run(args []string) int {
var config = common.OpenConfig()
arguments, err := clone.parse(args, config)
Expand Down
15 changes: 7 additions & 8 deletions clone/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ func TestCloneCommand_MultipleProjects(t *testing.T) {
if len(db.Repositories) != 4 {
t.Fatal("helloworld and fibonacci were not registered.")
}
var hwRepo = db.Repositories[2]
var hwRepo = db.Repositories[1]
if message := validate(hwRepo, "helloworld", "../testdata/hoge/helloworld"); message != "" {
t.Error(message)
}
var fiboRepo = db.Repositories[3]
var fiboRepo = db.Repositories[0]
if message := validate(fiboRepo, "fibonacci", "../testdata/hoge/fibonacci"); message != "" {
t.Error(message)
}
if !db.HasGroup("not-exist-group") || len(db.Groups) != 3 {
t.Fatalf("not-exist-group: group not found: %v", db.Groups)
}
var group = db.FindGroup("not-exist-group")
if !contains(group.Items, "helloworld") || !contains(group.Items, "fibonacci") {
if !db.HasRelation("not-exist-group", "helloworld") || !db.HasRelation("not-exist-group", "fibonacci") {
t.Errorf("%s: does not have helloworld or fibonacci", group.Name)
}
})
Expand All @@ -92,13 +92,12 @@ func TestCloneCommand_Run(t *testing.T) {
if len(db.Repositories) != 3 {
t.Fatal("helloworld was not registered.")
}
var repo = db.Repositories[2]
var repo = db.Repositories[0]
if message := validate(repo, "helloworld", "./helloworld"); message != "" {
t.Error(message)
}
var group = db.FindGroup("no-group")
if len(group.Items) != 1 || !contains(group.Items, "helloworld") {
t.Errorf("helloworld was not registered to the group \"no-group\": %v", group.Items)
if db.ContainsCount("no-group") != 1 || !db.HasRelation("no-group", "helloworld") {
t.Errorf("helloworld was not registered to the group \"no-group\": %v", db.Relations)
}
})
}
Expand All @@ -116,7 +115,7 @@ func TestCloneCommand_SpecifyingId(t *testing.T) {
if len(db.Repositories) != 3 {
t.Fatal("newid was not registered.")
}
var repo = db.Repositories[2]
var repo = db.Repositories[0]
if message := validate(repo, "newid", "../testdata/newid"); message != "" {
t.Error(message)
}
Expand Down
41 changes: 35 additions & 6 deletions common/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import (
"github.com/mitchellh/cli"
)

/*
ConfigCommand represents a command.
*/
type ConfigCommand struct{}
type configSetCommand struct{}
type configUnsetCommand struct{}
type configListCommand struct{}

/*
ConfigCommandFactory returns an instance of the ConfigCommand.
*/
func ConfigCommandFactory() (cli.Command, error) {
return &ConfigCommand{}, nil
}
Expand All @@ -28,6 +34,9 @@ func configListCommandFactory() (cli.Command, error) {
return &configListCommand{}, nil
}

/*
Help returns the help message.
*/
func (config *ConfigCommand) Help() string {
return `rrh config <COMMAND> [ARGUMENTS]
COMMAND
Expand All @@ -36,23 +45,35 @@ COMMAND
list list all of ENVs (default)`
}

/*
Help returns the help message.
*/
func (csc *configSetCommand) Help() string {
return `rrh config set <ENV_NAME> <VALUE>
ARGUMENTS
ENV_NAME environment name.
VALUE the value for the given environment.`
}

/*
Help returns the help message.
*/
func (cuc *configUnsetCommand) Help() string {
return `rrh config unset <ENV_NAME...>
ARGUMENTS
ENV_NAME environment name.`
}

/*
Help returns the help message.
*/
func (clc *configListCommand) Help() string {
return `rrh config list`
}

/*
Run performs the command.
*/
func (config *ConfigCommand) Run(args []string) int {
c := cli.NewCLI("rrh config", VERSION)
c.Args = args
Expand All @@ -65,15 +86,17 @@ func (config *ConfigCommand) Run(args []string) int {
if len(args) == 0 {
new(configListCommand).Run([]string{})
return 0
} else {
var exitStatus, err = c.Run()
if err != nil {
log.Println(err)
}
return exitStatus
}
var exitStatus, err = c.Run()
if err != nil {
log.Println(err)
}
return exitStatus
}

/*
Run performs the command.
*/
func (csc *configSetCommand) Run(args []string) int {
if len(args) != 2 {
fmt.Println(csc.Help())
Expand All @@ -89,6 +112,9 @@ func (csc *configSetCommand) Run(args []string) int {
return 0
}

/*
Run performs the command.
*/
func (cuc *configUnsetCommand) Run(args []string) int {
if len(args) != 1 {
fmt.Println(cuc.Help())
Expand All @@ -106,6 +132,9 @@ func (cuc *configUnsetCommand) Run(args []string) int {
return 0
}

/*
Run performs the command.
*/
func (clc *configListCommand) Run(args []string) int {
var config = OpenConfig()
fmt.Println(config.formatVariableAndValue(RrhHome))
Expand Down
Loading

0 comments on commit 9b83970

Please sign in to comment.