From ff2971b50c436a8c03691ddf1b8742ac5ef9a75c Mon Sep 17 00:00:00 2001 From: Siegfried Ehret Date: Sat, 20 Feb 2016 14:52:57 +0100 Subject: [PATCH] :wrench: a few updates after @vbehar comments --- README.md | 2 +- main.go | 44 +++++++++++++++++++++++++------------- pkg/gitconfig/gitconfig.go | 2 +- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d595afb..f677129 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Then, run `gitzytout` it will automagically configure your `.git/config` to allo ## todo -- [ ] Check with https/ssl urls +- [x] Check with https/ssl urls - [x] Check with passphrase keys - [x] [Add a main item](https://gitlab.com/SiegfriedEhret/gitzytout/issues/1) ? diff --git a/main.go b/main.go index 7a682ab..651823c 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io/ioutil" + "log" "strings" "gitlab.com/SiegfriedEhret/gitzytout/pkg/gitconfig" @@ -10,6 +11,10 @@ import ( "github.com/ghodss/yaml" ) +const ( + configPath string = "gitzytout.yaml" +) + // Config represents the yaml content type Config struct { Main string `json:"main"` @@ -29,52 +34,61 @@ func inArray(array []string, value string) bool { return false } -func maybeAddOrigin(main string) { +func maybeAddOrigin(main string) (err error) { remoteURL := gitconfig.GetRemoteOrigin() if strings.Compare(main, remoteURL) != 0 { - errMain := gitconfig.AddOrigin(main) - if errMain != nil { - fmt.Println("Error while adding origin: "+main, errMain.Error()) + if err := gitconfig.AddOrigin(main); err != nil { + fmt.Println("Error while adding origin: "+main, err.Error()) } } + + return } -func maybeAddPushUrls(main string, mirrors []string) { +func maybeAddPushUrls(main string, mirrors []string) (errors []error) { pushUrls := gitconfig.GetPushURL() - things := []string{main} things = append(things, mirrors...) for _, mirror := range things { if !inArray(pushUrls, mirror) { - err := gitconfig.AddPushURL(mirror) - if err != nil { - fmt.Println("Error while adding push url: `"+mirror, err.Error()) + if err := gitconfig.AddPushURL(mirror); err != nil { + errors = append(errors, err) } } } + + return } func main() { fmt.Println("gitzytout\n=========") - const configPath string = "gitzytout.yaml" - data, err := ioutil.ReadFile(configPath) if err != nil { - fmt.Printf("Can't read config %s", configPath) + log.Fatal("Can't read config " + configPath) } var conf Config unmarshalErr := yaml.Unmarshal(data, &conf) if unmarshalErr != nil { - fmt.Println("Can't decode yaml !") + log.Fatal("Can't decode yaml !") } - maybeAddOrigin(conf.Main) - maybeAddPushUrls(conf.Main, conf.Mirrors) + errMain := maybeAddOrigin(conf.Main) + errMirrors := maybeAddPushUrls(conf.Main, conf.Mirrors) + + if errMain != nil { + log.Println("Failed to set up the main repository", conf.Main) + } + + if len(errMirrors) > 0 { + for i := 0; i < len(errMirrors); i++ { + log.Println("Failed to write push url: ", errMirrors[i]) + } + } fmt.Println("Done!") } diff --git a/pkg/gitconfig/gitconfig.go b/pkg/gitconfig/gitconfig.go index e7cd1e1..477fe25 100644 --- a/pkg/gitconfig/gitconfig.go +++ b/pkg/gitconfig/gitconfig.go @@ -37,7 +37,7 @@ func GetPushURL() []string { fmt.Println("Something weird happend while reading available git push urls: ", err.Error(), "\nOutput is: ", output) } - stringOutput := string(output[:]) + stringOutput := string(output) stringArray := strings.Split(stringOutput, "\n") return stringArray -- 2.45.2