diff --git a/common/config.go b/common/config.go index 4d1a240..6105f67 100644 --- a/common/config.go +++ b/common/config.go @@ -10,6 +10,9 @@ import ( "github.com/mitchellh/go-homedir" ) +/* +VERSION shows the version of RRH. +*/ const VERSION = "0.2" const ( @@ -39,6 +42,9 @@ const ( Ignore = "IGNORE" ) +/* +Config shows the values of configuration variables. +*/ type Config struct { Home string `json:"rrh_home"` AutoDeleteGroup string `json:"rrh_auto_delete_group"` @@ -56,6 +62,10 @@ func (config *Config) isOnErrorIgnoreOrWarn() bool { return onError == Ignore || onError == Warn } +/* +PrintErrors prints errors and returns the status code by following the value of RrhOnError. +If the value of RrhOnError is Ignore or Warn, this method returns 0, otherwise, non-zero value. +*/ func (config *Config) PrintErrors(errs []error) int { if config.GetValue(RrhOnError) != Ignore { for _, err := range errs { @@ -85,6 +95,9 @@ func availableValueOnError(value string) (string, error) { return "", fmt.Errorf("%s: Unknown value of RRH_ON_ERROR (must be %s, %s, %s, or %s)", value, Fail, FailImmediately, Warn, Ignore) } +/* +Update method updates the config value with the given `value`. +*/ func (config *Config) Update(label string, value string) error { switch label { case RrhAutoDeleteGroup: @@ -124,11 +137,15 @@ func (config *Config) Update(label string, value string) error { } return err case RrhConfigPath: - return fmt.Errorf("%s: does not set on config file. set on environment.", label) + return fmt.Errorf("%s: does not set on config file. set on environment", label) } return fmt.Errorf("%s: Unknown variable name", label) } +/* +IsSet returns the bool value of the given label. +If the label is not RrhAutoCreateGroup, RrhAutoDeleteGroup, and RrhSortOnUpdating, this method always returns false. +*/ func (config *Config) IsSet(label string) bool { var value = config.GetValue(label) if label != RrhAutoCreateGroup && label != RrhAutoDeleteGroup && label != RrhSortOnUpdating { @@ -137,16 +154,25 @@ func (config *Config) IsSet(label string) bool { return strings.ToLower(value) == "true" } +/* +GetValue returns the value of the given variable name. +*/ func (config *Config) GetValue(label string) string { var value, _ = config.GetString(label) return value } +/* +GetDefaultValue returns the default value of the given variable name. +*/ func (config *Config) GetDefaultValue(label string) string { var value, _ = config.findDefaultValue(label) return value } +/* +GetString returns the value of the given variable name and the definition part of the value. +*/ func (config *Config) GetString(label string) (value string, readFrom string) { switch label { case RrhAutoDeleteGroup: @@ -215,6 +241,9 @@ func configPath() string { return home } +/* +StoreConfig saves the store. +*/ func (config *Config) StoreConfig() error { var configPath = config.GetValue(RrhConfigPath) var err1 = CreateParentDir(configPath) @@ -228,6 +257,10 @@ func (config *Config) StoreConfig() error { return err2 } +/* +OpenConfig reads the config file and returns it. +The load path is based on `RrhConfigPath` of the environment variables. +*/ func OpenConfig() *Config { bytes, err := ioutil.ReadFile(configPath()) if err != nil { diff --git a/common/config_test.go b/common/config_test.go index 660b9b5..8c1b758 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -80,7 +80,7 @@ func TestConfigUnset(t *testing.T) { baseConfig.StoreConfig() } -func ExampleConfigCommand_Run() { +func ExampleConfigCommand() { os.Setenv(RrhConfigPath, "../testdata/config.json") os.Setenv(RrhHome, "../testdata/") var command, _ = ConfigCommandFactory() @@ -96,7 +96,7 @@ func ExampleConfigCommand_Run() { // RRH_AUTO_DELETE_GROUP: false (config_file) // RRH_SORT_ON_UPDATING: true (config_file) } -func ExampleConfigCommand_Run_2() { +func ExampleConfigCommand_Run() { os.Setenv(RrhConfigPath, "../testdata/config.json") os.Setenv(RrhHome, "../testdata/") var command, _ = ConfigCommandFactory() diff --git a/common/database.go b/common/database.go index 5ba0638..a3d41d8 100644 --- a/common/database.go +++ b/common/database.go @@ -255,6 +255,9 @@ func (db *Database) ContainsCount(groupID string) int { return groups[groupID] } +/* +FindRelationsOfGroup returns the repository ids belonging to the specified group. +*/ func (db *Database) FindRelationsOfGroup(groupId string) []string { var repositories = []string{} for _, relation := range db.Relations { @@ -296,6 +299,9 @@ func (db *Database) Unrelate(groupID string, repoID string) { db.Relations = newRelations } +/* +UnrelateRepository deletes all relations of the specified repository. +*/ func (db *Database) UnrelateRepository(repoID string) { var newRelations = []Relation{} for _, relation := range db.Relations { @@ -306,6 +312,9 @@ func (db *Database) UnrelateRepository(repoID string) { db.Relations = newRelations } +/* +UnrelateGroup deletes all relations about the specified group. +*/ func (db *Database) UnrelateFromGroup(groupID string) { var newRelations = []Relation{} for _, relation := range db.Relations { diff --git a/common/rrhtime.go b/common/rrhtime.go index 3073521..de93904 100644 --- a/common/rrhtime.go +++ b/common/rrhtime.go @@ -5,6 +5,9 @@ import ( "time" ) +/* +RrhTime represents the time for RRH command, for marshaling a specific format. +*/ type RrhTime struct { time time.Time } diff --git a/fetch/fetch_all_cmd.go b/fetch/fetch_all_cmd.go index 512893d..b8f1a1f 100644 --- a/fetch/fetch_all_cmd.go +++ b/fetch/fetch_all_cmd.go @@ -58,7 +58,7 @@ func (fetch *FetchAllCommand) printError(errs []error) { } } -func (fetch *FetchAllCommand) execFetch(db *common.Database, fetchOptions *FetchOptions) int { +func (fetch *FetchAllCommand) execFetch(db *common.Database, fetchOptions *fetchOptions) int { var onError = db.Config.GetValue(common.RrhOnError) var fetch2 = FetchCommand{} @@ -84,8 +84,8 @@ func (fetch *FetchAllCommand) execFetch(db *common.Database, fetchOptions *Fetch return 0 } -func (fetch *FetchAllCommand) parse(args []string) (*FetchOptions, error) { - var options = FetchOptions{"origin", []string{}} +func (fetch *FetchAllCommand) parse(args []string) (*fetchOptions, error) { + var options = fetchOptions{"origin", []string{}} flags := flag.NewFlagSet("fetch-all", flag.ExitOnError) flags.Usage = func() { fmt.Println(fetch.Help()) } flags.StringVar(&options.remote, "r", "origin", "remote name") diff --git a/fetch/fetch_cmd.go b/fetch/fetch_cmd.go index 6d1ccad..415c78a 100644 --- a/fetch/fetch_cmd.go +++ b/fetch/fetch_cmd.go @@ -8,14 +8,23 @@ import ( "github.com/tamada/rrh/common" ) +/* +FetchCommand represents a command. +*/ type FetchCommand struct { - options *FetchOptions + options *fetchOptions } +/* +FetchCommandFactory returns an instance of command. +*/ func FetchCommandFactory() (cli.Command, error) { - return &FetchCommand{}, nil + return &FetchCommand{&fetchOptions{}}, nil } +/* +Help returns the help message of the command. +*/ func (fetch *FetchCommand) Help() string { return `rrh fetch [OPTIONS] [GROUPS...] OPTIONS @@ -25,10 +34,16 @@ ARGUMENTS if no value is specified, run on the default group.` } +/* +Synopsis returns the help message of the command. +*/ func (fetch *FetchCommand) Synopsis() string { return "run \"git fetch\" on the given groups." } +/* +Run performs the command. +*/ func (fetch *FetchCommand) Run(args []string) int { var fetchOptions, err = fetch.parse(args) if err != nil { @@ -65,7 +80,7 @@ func (fetch *FetchCommand) perform(db *common.Database) int { return errorFlag } -type FetchOptions struct { +type fetchOptions struct { remote string // key string // userName string @@ -73,8 +88,8 @@ type FetchOptions struct { args []string } -func (fetch *FetchCommand) parse(args []string) (*FetchOptions, error) { - var options = FetchOptions{"origin", []string{}} +func (fetch *FetchCommand) parse(args []string) (*fetchOptions, error) { + var options = fetchOptions{"origin", []string{}} flags := flag.NewFlagSet("fetch", flag.ExitOnError) flags.Usage = func() { fmt.Println(fetch.Help()) } flags.StringVar(&options.remote, "r", "origin", "remote name") diff --git a/list/list_cmd.go b/list/list_cmd.go index 22dfb87..848e131 100644 --- a/list/list_cmd.go +++ b/list/list_cmd.go @@ -98,21 +98,21 @@ func (options *listOptions) printRepo(repo Repo, result ListResult, formatString fmt.Println() } -func (options *listOptions) printSimple(result ListResult) bool { - if !options.noOmit && result.OmitList { - if len(result.Repos) == 1 { - fmt.Printf("%s (1 repository)\n", result.GroupName) - } else { - fmt.Printf("%s (%d repositories)\n", result.GroupName, len(result.Repos)) - } - return true +func (options *listOptions) isPrintSimple(result ListResult) bool { + return !options.noOmit && result.OmitList && len(options.args) == 0 +} + +func (options *listOptions) printRepositoryName(result ListResult) { + if len(result.Repos) == 1 { + fmt.Printf("%s (1 repository)\n", result.GroupName) + } else { + fmt.Printf("%s (%d repositories)\n", result.GroupName, len(result.Repos)) } - return false } func (options *listOptions) printResult(result ListResult) { - if !options.printSimple(result) { - fmt.Println(result.GroupName) + options.printRepositoryName(result) + if !options.isPrintSimple(result) { if options.description || options.all { fmt.Printf(" Description %s", result.Description) fmt.Println() diff --git a/list/list_test.go b/list/list_test.go index 1a36ea1..2198dc6 100644 --- a/list/list_test.go +++ b/list/list_test.go @@ -21,10 +21,10 @@ func ExampleListCommand_Run() { var list, _ = ListCommandFactory() list.Run([]string{"--desc", "--path"}) // Output: - // group1 + // group1 (1 repository) // Description desc1 // repo1 path1 - // group2 + // group2 (0 repositories) // Description desc2 // group3 (1 repository) }