Skip to content

Commit

Permalink
Added global gitconfig support to use command
Browse files Browse the repository at this point in the history
  • Loading branch information
dm3ch committed Jul 3, 2019
1 parent 35f2414 commit 273810d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 54 deletions.
1 change: 0 additions & 1 deletion cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

const (
profileExtention = "profile"
profileComment = "git-profile-manager: Do not remove"
)

// Create directory if it doesn't exists
Expand Down
26 changes: 16 additions & 10 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -27,24 +32,24 @@ 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)
os.Exit(1)
}
}

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)
Expand All @@ -55,4 +60,5 @@ var useCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(useCmd)
useCmd.Flags().BoolP("global", "g", false, "Set profile for global config")
}
65 changes: 33 additions & 32 deletions gitconfig/gitconfig.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 0 additions & 11 deletions gitconfig/helpers.go

This file was deleted.

0 comments on commit 273810d

Please sign in to comment.