forked from dotzero/git-profile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added global gitconfig support to use command
- Loading branch information
Showing
4 changed files
with
49 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.