From 273810d5bd1525bc6a215e5905ef8a9c4664d9d9 Mon Sep 17 00:00:00 2001 From: Dmitry Chepurovskiy Date: Wed, 3 Jul 2019 15:16:49 +0300 Subject: [PATCH] Added global gitconfig support to use command --- cmd/helpers.go | 1 - cmd/use.go | 26 ++++++++++------- gitconfig/gitconfig.go | 65 +++++++++++++++++++++--------------------- gitconfig/helpers.go | 11 ------- 4 files changed, 49 insertions(+), 54 deletions(-) delete mode 100644 gitconfig/helpers.go diff --git a/cmd/helpers.go b/cmd/helpers.go index f949f74..83ba775 100644 --- a/cmd/helpers.go +++ b/cmd/helpers.go @@ -13,7 +13,6 @@ import ( const ( profileExtention = "profile" - profileComment = "git-profile-manager: Do not remove" ) // Create directory if it doesn't exists diff --git a/cmd/use.go b/cmd/use.go index a9fbdaf..c8f5f90 100644 --- a/cmd/use.go +++ b/cmd/use.go @@ -3,21 +3,26 @@ package cmd import ( "fmt" "os" - "os/exec" + "github.com/dm3ch/git-profile-manager/gitconfig" "github.com/spf13/cobra" ) -type include struct { - Paths []string `ini:"path,omitempty,allowshadow"` -} - var useCmd = &cobra.Command{ Use: "use [profile name]", Short: "Use specified profile for current repo", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { profileName := args[0] + global, _ := cmd.Flags().GetBool("global") + + var configType gitconfig.ConfigType + if global { + configType = gitconfig.GlobalConfig + } else { + configType = gitconfig.LocalConfig + } + configDir := getConfigDirRelativePath() path := getProfilePath(configDir, profileName) @@ -27,9 +32,9 @@ var useCmd = &cobra.Command{ os.Exit(1) } - out, err := exec.Command("git", "config", "--get", "profile.path").CombinedOutput() - if err == nil { - out, err = exec.Command("git", "config", "--unset-all", "include.path", string(out[:len(out)-1])).CombinedOutput() + out, err := gitconfig.Get(configType, "profile.path") + if err == nil && out != "" { + out, err = gitconfig.UnsetAll(configType, "include.path", out[:len(out)-1]) if err != nil { fmt.Printf("git config command error:\n Output: %s\n", out) fmt.Println(err) @@ -37,14 +42,14 @@ var useCmd = &cobra.Command{ } } - out, err = exec.Command("git", "config", "--add", "include.path", path).CombinedOutput() + out, err = gitconfig.Add(configType, "include.path", path) if err != nil { fmt.Printf("git config command error:\n Output: %s\n", out) fmt.Println(err) os.Exit(1) } - out, err = exec.Command("git", "config", "--replace-all", "profile.path", path).CombinedOutput() + out, err = gitconfig.ReplaceAll(configType, "profile.path", path) if err != nil { fmt.Printf("git config command error:\n Output: %s\n", out) fmt.Println(err) @@ -55,4 +60,5 @@ var useCmd = &cobra.Command{ func init() { rootCmd.AddCommand(useCmd) + useCmd.Flags().BoolP("global", "g", false, "Set profile for global config") } diff --git a/gitconfig/gitconfig.go b/gitconfig/gitconfig.go index 13d1359..e7c0904 100644 --- a/gitconfig/gitconfig.go +++ b/gitconfig/gitconfig.go @@ -1,50 +1,51 @@ package gitconfig import ( - "github.com/go-ini/ini" + "errors" + "os/exec" ) -func loadConfig(path string) (*ini.File, error) { - cfg, err := ini.ShadowLoad(path) - return cfg, err -} - -func saveConfig(cfg *ini.File, path string) error { - return cfg.SaveTo(path) -} +type ConfigType int -func LoadLocalConfig() (*ini.File, error) { - path, err := getLocalConfigPath() - if err != nil { - return nil, err - } +const ( + LocalConfig ConfigType = iota + GlobalConfig + SystemConfig +) - return loadConfig(path) +func GitExec(command ...string) (string, error) { + out, err := exec.Command("git", command...).CombinedOutput() + return string(out), err } -func SaveLocalConfig(cfg *ini.File) error { - path, err := getLocalConfigPath() - if err != nil { - return err +func Exec(configType ConfigType, command ...string) (string, error) { + var args []string + switch configType { + case LocalConfig: + args = append([]string{"config", "--local"}, command...) + case GlobalConfig: + args = append([]string{"config", "--global"}, command...) + case SystemConfig: + args = append([]string{"config", "--system"}, command...) + default: + return "", errors.New("can't recognize ConfigType") } - return saveConfig(cfg, path) + return GitExec(args...) } -func LoadGlobalConfig() (*ini.File, error) { - path, err := getGlobalConfigPath() - if err != nil { - return nil, err - } +func ReplaceAll(configType ConfigType, key, value string) (string, error) { + return Exec(configType, "--replace-all", key, value) +} - return loadConfig(path) +func Add(configType ConfigType, key, value string) (string, error) { + return Exec(configType, "--add", key, value) } -func SaveGlobalConfig(cfg *ini.File) error { - path, err := getGlobalConfigPath() - if err != nil { - return err - } +func UnsetAll(configType ConfigType, key, value string) (string, error) { + return Exec(configType, "--unset-all", key, value) +} - return saveConfig(cfg, path) +func Get(configType ConfigType, key string) (string, error) { + return Exec(configType, "--get", key) } diff --git a/gitconfig/helpers.go b/gitconfig/helpers.go deleted file mode 100644 index 0e1b777..0000000 --- a/gitconfig/helpers.go +++ /dev/null @@ -1,11 +0,0 @@ -package gitconfig - -import "github.com/mitchellh/go-homedir" - -func getGlobalConfigPath() (string, error) { - return homedir.Expand("~/.gitconfig") -} - -func getLocalConfigPath() (string, error) { - return ".git/config", nil -}