Skip to content

Commit

Permalink
Merge 3236e2c into ab14cd2
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Apr 2, 2019
2 parents ab14cd2 + 3236e2c commit abcf05a
Show file tree
Hide file tree
Showing 18 changed files with 637 additions and 65 deletions.
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ RRH is now growing. Please hack RRH itself.
* [`rrh import`](#rrh-import)
* [`rrh list`](#rrh-list)
* [`rrh mv`](#rrh-mv)
* [`rrh path`](#rrh-path)
* [`rrh prune`](#rrh-prune)
* [`rrh rm`](#rrh-rm)
* [`rrh status`](#rrh-status)
* [Environment variables](#environment-variables)
* [Database](#database)
* [Utilities](#utilities)
* [`cdrrh`](#cdrrh)
* [`rrhpeco`](#rrhpeco)
* [Development Policy](#development-policy)
* [Why the project name RRH?](#why-the-project-name-rrh)
* [Discussion](#discussion)
Expand Down Expand Up @@ -87,6 +89,7 @@ Available commands are:
group add/list/update/remove groups.
list print managed repositories and their groups.
mv move the repositories from groups to another group.
path print paths of specified repositories.
prune prune unnecessary repositories and groups.
rm remove given repository from database.
status show git status of repositories.
Expand Down Expand Up @@ -238,7 +241,8 @@ OPTIONS
-d, --desc <DESC> change description to DESC.
-o, --omit-list <FLAG> change omit-list of the group. FLAG must be "true" or "false".
ARGUMENTS
GROUP update target group names.```
GROUP update target group names.
```
### `rrh import`
Expand Down Expand Up @@ -287,6 +291,19 @@ ARGUMENTS
TO specifies move to, formatted in <GROUP_NAME>
```
### `rrh path`
Prints paths of the specified repositories.
```sh
rrh path [OPTIONS] <REPOSITORIES...>
OPTIONS
-m, --partial-match treats the arguments as the patterns.
-p, --show-only-path show path only.
ARGUMENTS
REPOSITORIES repository ids.
```
### `rrh prune`
Deletes unnecessary groups and repositories.
Expand All @@ -304,7 +321,7 @@ Removes the specified groups, repositories, and relations.
If the group has entries is removed by specifying the option `--recursive.`
```sh
rrh rm [OPTIONS] <REPO_ID|GROUP_ID|REPO_ID/GROUP_ID...>
rrh rm [OPTIONS] <REPO_ID|GROUP_ID|GROUP_ID/REPO_ID...>
OPTIONS
-i, --inquiry inquiry mode.
-r, --recursive recursive mode.
Expand All @@ -322,10 +339,10 @@ ARGUMENTS
Prints the last modified times of each branch in the repositories of the specified group.
```sh
rrh status [OPTIONS] [GROUPS||REPOS...]
rrh status [OPTIONS] [GROUPS|REPOS...]
OPTIONS
-b, --branches show the status of the local branches.
-r, --remote show the status of the remote branches.
-r, --remote show the status of the remote branches.
-c, --csv print result in csv format.
ARGUMENTS
GROUPS target groups.
Expand Down Expand Up @@ -431,11 +448,21 @@ Also, the configuration file is on `$RRH_ROOT/config.json`
## `cdrrh`
changes directory to the specified repository.
```sh
cdrrh(){
cd $(rrh path -p $1)
}
```
## `rrhpeco`
list repositories, and filtering them by [`peco`](https://github.com/peco/peco),
then change directory to the filtering result.
```sh
cdrrh(){
rrhpeco(){
csv=$(rrh list --path --csv | peco)
cd $(echo $csv | awk -F , '{ print $3 }')
pwd
Expand Down
4 changes: 2 additions & 2 deletions clone/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestCloneCommand_MultipleProjects(t *testing.T) {

var config = common.OpenConfig()
var db, _ = common.Open(config)
if len(db.Repositories) != 4 {
if !db.HasRepository("helloworld") && !db.HasRepository("fibonacci") {
t.Fatal("helloworld and fibonacci were not registered.")
}
var hwRepo = db.FindRepository("helloworld")
Expand All @@ -69,7 +69,7 @@ func TestCloneCommand_MultipleProjects(t *testing.T) {
if message := validate(*fiboRepo, "fibonacci", "../testdata/hoge/fibonacci"); message != "" {
t.Error(message)
}
if !db.HasGroup("not-exist-group") || len(db.Groups) != 4 {
if !db.HasGroup("not-exist-group") {
t.Fatalf("not-exist-group: group not found: %v", db.Groups)
}
var group = db.FindGroup("not-exist-group")
Expand Down
30 changes: 21 additions & 9 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,25 @@ func (config *Config) IsSet(label string) bool {
return false
}

func (config *Config) replaceHome(value string, rrhHomeGetter func(*Config) string) string {
if strings.Contains(value, "${HOME}") {
var home, _ = homedir.Dir()
value = strings.Replace(value, "${HOME}", home, 1)
}
if strings.Contains(value, "${RRH_HOME}") {
value = strings.Replace(value, "${RRH_HOME}", rrhHomeGetter(config), 1)
}
return value
}

/*
GetValue returns the value of the given variable name.
*/
func (config *Config) GetValue(label string) string {
var value, _ = config.GetString(label)
return value
return config.replaceHome(value, func(c *Config) string {
return c.GetValue(RrhHome)
})
}

/*
Expand All @@ -212,30 +225,29 @@ GetDefaultValue returns the default value of the given variable name.
*/
func (config *Config) GetDefaultValue(label string) string {
var value, _ = config.findDefaultValue(label)
if strings.Contains(value, "${HOME}") {
var home, _ = homedir.Dir()
value = strings.Replace(value, "${HOME}", home, 1)
}
if strings.Contains(value, "${RRH_HOME}") {
value = strings.Replace(value, "${RRH_HOME}", config.GetDefaultValue(RrhHome), 1)
}
return value
}

func (config *Config) getStringFromEnv(label string) (string, ReadFrom) {
var valueFromEnv = os.Getenv(label)
if valueFromEnv != "" {
valueFromEnv = config.replaceHome(valueFromEnv, func(c *Config) string {
var val, _ = c.getStringFromEnv(RrhHome)
return val
})
return valueFromEnv, Env
}
return config.findDefaultValue(label)

}

func (config *Config) findDefaultValue(label string) (string, ReadFrom) {
if !contains(availableLabels, label) {
return "", NotFound
}
var value = defaultValues[label]
value = config.replaceHome(value, func(c *Config) string {
return c.GetDefaultValue(RrhHome)
})
return value, Default
}

Expand Down
13 changes: 3 additions & 10 deletions common/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,9 @@ Run performs the command.
*/
func (clc *configListCommand) Run(args []string) int {
var config = OpenConfig()
fmt.Println(config.formatVariableAndValue(RrhHome))
fmt.Println(config.formatVariableAndValue(RrhConfigPath))
fmt.Println(config.formatVariableAndValue(RrhDatabasePath))
fmt.Println(config.formatVariableAndValue(RrhDefaultGroupName))
fmt.Println(config.formatVariableAndValue(RrhOnError))
fmt.Println(config.formatVariableAndValue(RrhAutoCreateGroup))
fmt.Println(config.formatVariableAndValue(RrhAutoDeleteGroup))
fmt.Println(config.formatVariableAndValue(RrhTimeFormat))
fmt.Println(config.formatVariableAndValue(RrhCloneDestination))
fmt.Println(config.formatVariableAndValue(RrhSortOnUpdating))
for _, label := range availableLabels {
fmt.Println(config.formatVariableAndValue(label))
}
return 0
}

Expand Down
30 changes: 15 additions & 15 deletions common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,50 +103,50 @@ func ExampleConfigCommand() {
var command, _ = ConfigCommandFactory()
command.Run([]string{}) // the output of no arguments are same as list subcommand.
// Output:
// RRH_HOME: ../testdata/ (environment)
// RRH_AUTO_CREATE_GROUP: true (config_file)
// RRH_AUTO_DELETE_GROUP: false (config_file)
// RRH_CLONE_DESTINATION: . (default)
// RRH_CONFIG_PATH: ../testdata/config.json (environment)
// RRH_DATABASE_PATH: ../testdata/database.json (environment)
// RRH_DEFAULT_GROUP_NAME: no-group (default)
// RRH_HOME: ../testdata/ (environment)
// RRH_ON_ERROR: WARN (default)
// RRH_AUTO_CREATE_GROUP: true (config_file)
// RRH_AUTO_DELETE_GROUP: false (config_file)
// RRH_TIME_FORMAT: relative (default)
// RRH_CLONE_DESTINATION: . (default)
// RRH_SORT_ON_UPDATING: true (config_file)
// RRH_TIME_FORMAT: relative (default)
}
func ExampleConfigCommand_Run() {
os.Setenv(RrhConfigPath, "../testdata/config.json")
os.Setenv(RrhHome, "../testdata/")
var command, _ = ConfigCommandFactory()
command.Run([]string{"list"}) // the output of no arguments are same as list subcommand.
// Output:
// RRH_HOME: ../testdata/ (environment)
// RRH_AUTO_CREATE_GROUP: true (config_file)
// RRH_AUTO_DELETE_GROUP: false (config_file)
// RRH_CLONE_DESTINATION: . (default)
// RRH_CONFIG_PATH: ../testdata/config.json (environment)
// RRH_DATABASE_PATH: ../testdata/database.json (environment)
// RRH_DEFAULT_GROUP_NAME: no-group (default)
// RRH_HOME: ../testdata/ (environment)
// RRH_ON_ERROR: WARN (default)
// RRH_AUTO_CREATE_GROUP: true (config_file)
// RRH_AUTO_DELETE_GROUP: false (config_file)
// RRH_TIME_FORMAT: relative (default)
// RRH_CLONE_DESTINATION: . (default)
// RRH_SORT_ON_UPDATING: true (config_file)
// RRH_TIME_FORMAT: relative (default)
}
func Example_configListCommand_Run() {
os.Setenv(RrhConfigPath, "../testdata/config.json")
os.Setenv(RrhHome, "../testdata/")
var clc, _ = configListCommandFactory()
clc.Run([]string{})
// Output:
// RRH_HOME: ../testdata/ (environment)
// RRH_AUTO_CREATE_GROUP: true (config_file)
// RRH_AUTO_DELETE_GROUP: false (config_file)
// RRH_CLONE_DESTINATION: . (default)
// RRH_CONFIG_PATH: ../testdata/config.json (environment)
// RRH_DATABASE_PATH: ../testdata/database.json (environment)
// RRH_DEFAULT_GROUP_NAME: no-group (default)
// RRH_HOME: ../testdata/ (environment)
// RRH_ON_ERROR: WARN (default)
// RRH_AUTO_CREATE_GROUP: true (config_file)
// RRH_AUTO_DELETE_GROUP: false (config_file)
// RRH_TIME_FORMAT: relative (default)
// RRH_CLONE_DESTINATION: . (default)
// RRH_SORT_ON_UPDATING: true (config_file)
// RRH_TIME_FORMAT: relative (default)
}

func TestOpenConfigBrokenJson(t *testing.T) {
Expand Down

0 comments on commit abcf05a

Please sign in to comment.