-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfoo.go
42 lines (34 loc) · 835 Bytes
/
foo.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
package foo
import (
"fmt"
"os/exec"
"strings"
)
type IShellCommand interface {
SetDir(string)
Output() ([]byte, error)
Wait() error
}
type execShellCommand struct {
*exec.Cmd
}
func (exc execShellCommand) SetDir(dir string) {
exc.Dir = dir
}
func newExecShellCommander(name string, arg ...string) IShellCommand {
execCmd := exec.Command(name, arg...)
return execShellCommand{Cmd: execCmd}
}
// override this in tests to mock the git shell command
var shellCommander = newExecShellCommander
func myFuncThatUsesExecCmd() {
cmd := shellCommander("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.SetDir("mydir")
output, err := cmd.Output()
if err != nil {
fmt.Println("Git rev-parse failed")
return
}
gitCurrentBranch := strings.TrimSpace(string(output))
fmt.Printf("Git branch is '%v'\n", gitCurrentBranch)
}