Skip to content

Commit

Permalink
Can print version and help when configuration is not present
Browse files Browse the repository at this point in the history
File ~/.parallel-git-repo is only read (and fail) when necessary.
One can use -h and -v option when the configuration file is not yet present.

Fixes #3
  • Loading branch information
jcgay committed Aug 18, 2017
1 parent ff7db2a commit a5ef56d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
42 changes: 28 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func main() {
log.Fatalf("Cannot read user home directory.\n%v", err)
}
}
configuration := newConfiguration(home)

flag.BoolVar(&quiet, "q", false, "do not print stdout commands result, only stderr will be shown")
flag.BoolVar(&printVersion, "v", false, "print current version")
Expand All @@ -61,29 +60,36 @@ func main() {
flag.StringVar(&group, "g", "default", "execute command for a specific repositories group")

flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(help, version.VERSION, version.GITCOMMIT, listCommands(configuration)))
fmt.Fprint(os.Stderr, fmt.Sprintf(help, version.VERSION, version.GITCOMMIT, listCommands()))
flag.PrintDefaults()
}

flag.Parse()

if printVersion {
fmt.Printf("version: %s (%s)", version.VERSION, version.GITCOMMIT)
} else if os.Args[1] == "list" {
repos := configuration.ListRepositories()
for key, repos := range repos {
fmt.Printf("%s:\n", key)
for _, repo := range repos {
fmt.Printf(" - %s\n", repo)
} else {
configuration := newConfiguration(home)
if os.Args[1] == "list" {
repos := configuration.ListRepositories()
for key, repos := range repos {
fmt.Printf("%s:\n", key)
for _, repo := range repos {
fmt.Printf(" - %s\n", repo)
}
}
} else {
runCommand(configuration, withoutFlags(os.Args[1:]), group)
}
} else {
runCommand(configuration, withoutFlags(os.Args[1:]), group)
}
}

func listCommands(config *configuration) string {
commands := config.ListCommands()
func listCommands() string {
config, err := tryNewConfiguration(home)
commands := make(map[string]string)
if err == nil {
commands = config.ListCommands()
}

maxSize := 3
for key := range commands {
Expand Down Expand Up @@ -136,11 +142,19 @@ type configuration struct {
}

func newConfiguration(homeDir string) *configuration {
config, err := toml.LoadFile(homeDir + "/.parallel-git-repositories")
config, err := tryNewConfiguration(homeDir)
if err != nil {
log.Fatalf("Can't read configuration file %s/.parallel-git-repositories, verify that the file is valid...\n%v", homeDir, err)
}
return &configuration{config}
return config
}

func tryNewConfiguration(homeDir string) (*configuration, error) {
config, err := toml.LoadFile(homeDir + "/.parallel-git-repositories")
if err != nil {
return nil, err
}
return &configuration{config}, nil
}

func (config *configuration) ListRepositories() map[string][]string {
Expand Down
3 changes: 0 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
)

func Example() {
home, _ = ioutil.TempDir("", "pgr")
ioutil.WriteFile(home+"/.parallel-git-repositories", []byte(""), 0644)

os.Args = []string{"parallel-git-repo", "-v"}

main()
Expand Down

0 comments on commit a5ef56d

Please sign in to comment.