/
use.go
56 lines (44 loc) · 1019 Bytes
/
use.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
53
54
55
56
package main
import (
"fmt"
"os"
"os/exec"
"github.com/mitchellh/cli"
)
func isGitRootDir() bool {
stat, err := os.Stat(".git")
return err == nil && stat.IsDir()
}
type UseCommand struct {
ui cli.Ui
}
func (c *UseCommand) Synopsis() string {
return "Use selected profile"
}
func (c *UseCommand) Help() string {
return "Usage: git profile use <profile-name>"
}
func (c *UseCommand) Run(args []string) int {
if len(args) == 0 {
c.ui.Output(c.Help())
return 1
}
if !isGitRootDir() {
c.ui.Error("not git repository or isn't root")
return 1
}
profile, err := profiles.Get(args[0])
if err != nil {
c.ui.Error(fmt.Sprint(err))
return 1
}
if err := exec.Command("git", "config", "--local", "user.name", profile.Name).Run(); err != nil {
c.ui.Error(fmt.Sprintf("execution error: %s", err))
return 1
}
if err := exec.Command("git", "config", "--local", "user.email", profile.Email).Run(); err != nil {
c.ui.Error(fmt.Sprintf("execution error: %s", err))
return 1
}
return 0
}