You've already forked panic-cli
mirror of
https://github.com/Print-and-Panic/panic-cli.git
synced 2026-01-21 10:17:41 -08:00
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package git
|
|
|
|
import (
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
type GitIdentity struct {
|
|
Name string
|
|
Email string
|
|
}
|
|
|
|
type DefaultGitProvider struct{}
|
|
|
|
func (d *DefaultGitProvider) SetGitIdentity(gitIdentity GitIdentity) error {
|
|
cmd := exec.Command("git", "config", "--global", "user.name", gitIdentity.Name)
|
|
if err := cmd.Run(); err != nil {
|
|
return err
|
|
}
|
|
return exec.Command("git", "config", "--global", "user.email", gitIdentity.Email).Run()
|
|
}
|
|
|
|
func (d *DefaultGitProvider) GetCurrentGitIdentity() (GitIdentity, error) {
|
|
nameOut, err := exec.Command("git", "config", "user.name").Output()
|
|
if err != nil {
|
|
return GitIdentity{}, err
|
|
}
|
|
|
|
emailOut, err := exec.Command("git", "config", "user.email").Output()
|
|
if err != nil {
|
|
return GitIdentity{}, err
|
|
}
|
|
|
|
return GitIdentity{
|
|
Name: strings.TrimSpace(string(nameOut)),
|
|
Email: strings.TrimSpace(string(emailOut)),
|
|
}, nil
|
|
}
|
|
|
|
func (d *DefaultGitProvider) GetLocalGitIdentity() (GitIdentity, error) {
|
|
// Check if inside a git repo first to avoid noisy errors
|
|
if err := exec.Command("git", "rev-parse", "--is-inside-work-tree").Run(); err != nil {
|
|
return GitIdentity{}, nil // Not a git repo, so no local config
|
|
}
|
|
|
|
// We ignore errors here because if the key isn't set, git exits with 1
|
|
nameOut, _ := exec.Command("git", "config", "--local", "user.name").Output()
|
|
emailOut, _ := exec.Command("git", "config", "--local", "user.email").Output()
|
|
|
|
return GitIdentity{
|
|
Name: strings.TrimSpace(string(nameOut)),
|
|
Email: strings.TrimSpace(string(emailOut)),
|
|
}, nil
|
|
}
|