Skip to content

Commit

Permalink
Merge 201ee29 into 8f18728
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Mar 31, 2019
2 parents 8f18728 + 201ee29 commit a43a881
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 24 deletions.
35 changes: 34 additions & 1 deletion common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"github.com/mitchellh/go-homedir"
)

/*
VERSION shows the version of RRH.
*/
const VERSION = "0.2"

const (
Expand Down Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions common/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions common/rrhtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"time"
)

/*
RrhTime represents the time for RRH command, for marshaling a specific format.
*/
type RrhTime struct {
time time.Time
}
Expand Down
6 changes: 3 additions & 3 deletions fetch/fetch_all_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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")
Expand Down
25 changes: 20 additions & 5 deletions fetch/fetch_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -65,16 +80,16 @@ func (fetch *FetchCommand) perform(db *common.Database) int {
return errorFlag
}

type FetchOptions struct {
type fetchOptions struct {
remote string
// key string
// userName string
// password string
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")
Expand Down
22 changes: 11 additions & 11 deletions list/list_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a43a881

Please sign in to comment.