-
Notifications
You must be signed in to change notification settings - Fork 4
/
testutils_command.go
43 lines (38 loc) · 1.01 KB
/
testutils_command.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
package internal
import (
"strings"
"github.com/salsadigitalauorg/shipshape/pkg/command"
)
type TestShellCommand struct {
OutputterFunc func() ([]byte, error)
}
func (sc TestShellCommand) Output() ([]byte, error) {
return sc.OutputterFunc()
}
// ShellCommanderMaker is a commander generator that can return the provided
// stdout or stderr, and can also update a given variable with the generated
// command.
func ShellCommanderMaker(out *string, err error, generatedCommand *string) func(name string, arg ...string) command.IShellCommand {
return func(name string, arg ...string) command.IShellCommand {
if generatedCommand != nil {
fullCmd := name
for _, a := range arg {
// Add quotes when there are spaces.
if len(strings.Fields(a)) > 1 {
a = "'" + a + "'"
}
fullCmd += " " + a
}
*generatedCommand = fullCmd
}
var stdout []byte
if out != nil {
stdout = []byte(*out)
}
return TestShellCommand{
OutputterFunc: func() ([]byte, error) {
return stdout, err
},
}
}
}