-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.go
45 lines (37 loc) · 960 Bytes
/
program.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
package wrapper
import (
"fmt"
"os/exec"
)
type Program struct {
executable string
params map[string]interface{}
getCombinedOutputFunc func(cmd *exec.Cmd) ([]byte, error)
}
func NewProgram(executable string) *Program {
program := &Program{
executable: executable,
params: make(map[string]interface{}),
}
program.getCombinedOutputFunc = program.getCombinedOutput
return program
}
func (p *Program) WithParam(name string, value interface{}) *Program {
p.params[name] = value
return p
}
func (p *Program) getCombinedOutput(cmd *exec.Cmd) ([]byte, error) {
return cmd.CombinedOutput()
}
func (p *Program) Run() (string, error) {
var params []string
for name, value := range p.params {
params = append(params, "-"+name, fmt.Sprintf("%v", value))
}
cmd := exec.Command(p.executable, params...)
output, err := p.getCombinedOutputFunc(cmd)
if err != nil {
return "", err
}
return string(output), nil
}