-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
git.go
50 lines (44 loc) · 979 Bytes
/
git.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
package tagpr
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
)
type commander struct {
outStream, errStream io.Writer
gitPath, dir string
}
func (c *commander) getGitPath() string {
if c.gitPath == "" {
return "git"
}
return c.gitPath
}
func (c *commander) Cmd(prog string, args []string, env map[string]string) (string, string, error) {
log.Println(prog, args)
var (
outBuf bytes.Buffer
errBuf bytes.Buffer
)
cmd := exec.Command(prog, args...)
if env != nil {
cmd.Env = os.Environ()
for k, v := range env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}
}
cmd.Stdout = io.MultiWriter(&outBuf, c.outStream)
cmd.Stderr = io.MultiWriter(&errBuf, c.errStream)
if c.dir != "" {
cmd.Dir = c.dir
}
err := cmd.Run()
return strings.TrimSpace(outBuf.String()), strings.TrimSpace(errBuf.String()), err
}
func (c *commander) Git(args ...string) (string, string, error) {
return c.Cmd(c.getGitPath(), args, nil)
}