/
ambassador.go
49 lines (39 loc) · 1.01 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
package ext
import (
"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, 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) ([]byte, error) {
return cmd.CombinedOutput()
}
func (a *ambassador) TempFile(dir, pattern string) (File, error) {
return os.CreateTemp(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)
}