forked from ribice/glice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_user.go
52 lines (43 loc) · 968 Bytes
/
git_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package glice
import (
"os/exec"
"strings"
)
var _ UserAdapter = (*GitUser)(nil)
type GitUser struct {
Name string
Email string
}
func (gu *GitUser) GetName() string {
return gu.Name
}
func (gu *GitUser) GetEmail() string {
return gu.Email
}
var user *GitUser
func GetGitUser() UserAdapter {
var cmd *exec.Cmd
var name, email []byte
var err error
if user != nil {
goto end
}
cmd = exec.Command("git", "config", "--default", defaultName, "user.name")
name, err = cmd.CombinedOutput()
if err != nil {
Warnf("Failed when calling `git config`; %s", err.Error())
name = []byte(defaultName)
}
cmd = exec.Command("git", "config", "--default", defaultEmail, "user.email")
email, err = cmd.CombinedOutput()
if err != nil {
Warnf("Failed when calling `git config`; %s", err.Error())
email = []byte(defaultName)
}
user = &GitUser{
Name: strings.TrimSpace(string(name)),
Email: strings.TrimSpace(string(email)),
}
end:
return user
}