/
ambassador.go
52 lines (42 loc) · 1.1 KB
/
ambassador.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 ext
import (
"io/ioutil"
"os"
"os/exec"
)
var (
DefaultAmbassador = &ambassador{}
)
// File abstracts the few methods we need, so we can test without real files.
type File interface {
Name() string
Read([]byte) (int, error)
}
// Ambassador the ambassador to the outside "world". Wraps methods that modify global state and hence make the code that
// use them very hard to test.
type Ambassador interface {
Environ() []string
LookPath(string) (string, error)
RunCmd(cmd *exec.Cmd) ([]byte, int, error)
TempFile(dir, pattern string) (File, error)
Remove(name string) error
}
type ambassador struct {
}
func (a *ambassador) Environ() []string {
return os.Environ()
}
func (a *ambassador) RunCmd(cmd *exec.Cmd) (output []byte, code int, err error) {
output, err = cmd.CombinedOutput()
code = cmd.ProcessState.ExitCode()
return
}
func (a *ambassador) TempFile(dir, pattern string) (File, error) {
return ioutil.TempFile(dir, pattern)
}
func (a *ambassador) Remove(name string) error {
return os.Remove(name)
}
func (a *ambassador) LookPath(file string) (string, error) {
return exec.LookPath(file)
}