~siegfriedehret/gitzytout

3d98e05028b14209e296efc1779548e8f80a4bf5 — Siegfried Ehret 8 years ago 90870e0
:gift: add git config info retrieval !
2 files changed, 92 insertions(+), 21 deletions(-)

M main.go
A pkg/gitconfig/gitconfig.go
M main.go => main.go +37 -21
@@ 3,34 3,58 @@ package main
import (
	"fmt"
	"io/ioutil"
	"os/exec"
	"os/user"
	"strings"

	"gitlab.com/gitzytout/pkg/gitconfig"

	"github.com/ghodss/yaml"
)

// Config represents the yaml content
type Config struct {
	Main    string   `json:"main"`
	Mirrors []string `json:"mirrors"`
}

func (c Config) String() string {
	return strings.Join(c.Mirrors, ", ")
	return c.Main + "; " + strings.Join(c.Mirrors, ", ")
}

func GetUserDir() string {
	usr, err := user.Current()
	if err != nil {
		fmt.Println("!! Can't get current user", err)
		return ""
func inArray(array []string, value string) bool {
	for _, v := range array {
		if strings.Compare(value, v) == 0 {
			return true
		}
	}
	return false
}

	return usr.HomeDir
func maybeAddOrigin(main string) {
	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())
		}
	}
}

func maybeAddPushUrls(mirrors []string) {
	pushUrls := gitconfig.GetPushURL()

	for _, mirror := range mirrors {
		if !inArray(pushUrls, mirror) {
			err := gitconfig.AddPushURL(mirror)
			if err != nil {
				fmt.Println("Error while adding push url: `"+mirror, err.Error())
			}
		}
	}
}

func main() {
	fmt.Println("  gitzytout\n  =========")
	fmt.Println("gitzytout\n=========")

	const configPath string = "gitzytout.yaml"



@@ 46,16 70,8 @@ func main() {
		fmt.Println("Can't decode yaml !")
	}

	fmt.Println(conf)
	maybeAddOrigin(conf.Main)
	maybeAddPushUrls(conf.Mirrors)

	// git remote set-url origin --push --add git@gitlab.com:SiegfriedEhret/test.git
	// git remote set-url origin --push --add git@github.com:SiegfriedEhret/test.git

	for _, mirror := range conf.Mirrors {
		cmd := exec.Command("git", "remote", "set-url", "origin", "--push", "--add", mirror)
		err := cmd.Run()
		if err != nil {
			fmt.Println("Error while running `" + strings.Join(cmd.Args, " ") + "`")
		}
	}
	fmt.Println("Done!")
}

A pkg/gitconfig/gitconfig.go => pkg/gitconfig/gitconfig.go +55 -0
@@ 0,0 1,55 @@
// Package gitconfig expose a few methods to interact with a git configuration
package gitconfig

import (
	"fmt"
	"os/exec"
	"strings"
)

// AddOrigin sets the remote origin
func AddOrigin(main string) error {
	output, err := exec.Command("git", "remote", "add", "origin", main).Output()

	if err != nil && len(output) != 0 {
		fmt.Println("Something weird happend while adding origin", err.Error(), "\nOutput is:", output)
	}

	return err
}

// AddPushURL adds a url to push your code
func AddPushURL(mirror string) error {
	output, err := exec.Command("git", "remote", "set-url", "origin", "--push", "--add", mirror).Output()

	if err != nil && len(output) != 0 {
		fmt.Println("Something weird happend while adding push url: ", err.Error(), "\nOutput is:", output)
	}

	return err
}

// GetPushURL returns an array of available push urls
func GetPushURL() []string {
	output, err := exec.Command("git", "config", "--get-all", "remote.origin.pushurl").Output()

	if err != nil && len(output) != 0 {
		fmt.Println("Something weird happend while reading available git push urls: ", err.Error(), "\nOutput is: ", output)
	}

	stringOutput := string(output[:])
	stringArray := strings.Split(stringOutput, "\n")

	return stringArray
}

// GetRemoteOrigin returns the configured remote origin url
func GetRemoteOrigin() string {
	output, err := exec.Command("git", "config", "--get", "remote.origin.url").Output()

	if err != nil && len(output) != 0 {
		fmt.Println("Something weird happend while reading available remote origin", err.Error(), "\nOutput is: ", output)
	}

	return strings.Trim(string(output), "\n")
}