-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
123 lines (102 loc) · 2.45 KB
/
exec.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"io"
"os/exec"
"strings"
)
// CommandExecutor interface for os command execution
type CommandExecutor interface {
GetArgs() []string
Run() error
Success() bool
SetStdout(wr io.Writer)
SetStderr(wr io.Writer)
SetEnv(env []string)
}
// CommandCreator constructer interface
type CommandCreator func(context.Context, string, ...string) CommandExecutor
var _ CommandExecutor = (*OsCommand)(nil)
// NewOsCommand returns real command executor
func NewOsCommand(ctx context.Context, bin string, args ...string) CommandExecutor {
cmd := exec.CommandContext(ctx, bin, args...)
return &OsCommand{cmd}
}
// OsCommand wrapper for exec.Cmd
type OsCommand struct {
*exec.Cmd
}
// GetArgs returns all command arguments
func (c *OsCommand) GetArgs() []string {
return c.Cmd.Args
}
// Success returns true if execution returned 0 exit code
func (c *OsCommand) Success() bool {
// cmd maybe killed by canceled ctx
if c.Cmd.ProcessState != nil {
return c.Cmd.ProcessState.Success()
}
return false
}
// SetStdout settter
func (c *OsCommand) SetStdout(wr io.Writer) {
c.Cmd.Stdout = wr
}
// SetStderr settter
func (c *OsCommand) SetStderr(wr io.Writer) {
c.Cmd.Stderr = wr
}
// SetEnv setter
func (c *OsCommand) SetEnv(env []string) {
c.Cmd.Env = env
}
var _ CommandExecutor = (*MockCommand)(nil)
// MockCommand mock executor for testing
// implements CommandExecutor interface
type MockCommand struct {
bin string
args []string
env []string
stdOut, stdErr io.Writer
success bool
error error
execLog []string
}
// NewMockCommand returns preconfigured command
// with errors and success status
func NewMockCommand(err error, success bool) MockCommand {
return MockCommand{
error: err, success: success,
}
}
// New --
func (c *MockCommand) New(ctx context.Context, bin string, args ...string) CommandExecutor {
c.bin = bin
c.args = args
c.execLog = append(c.execLog, bin+" "+strings.Join(args, " "))
return c
}
// GetArgs --
func (c *MockCommand) GetArgs() []string {
return append([]string{c.bin}, c.args...)
}
// Run --
func (c *MockCommand) Run() error {
return c.error
}
// SetStdout --
func (c *MockCommand) SetStdout(wr io.Writer) {
c.stdOut = wr
}
// SetStderr --
func (c *MockCommand) SetStderr(wr io.Writer) {
c.stdErr = wr
}
// SetEnv --
func (c *MockCommand) SetEnv(env []string) {
c.env = env
}
// Success --
func (c *MockCommand) Success() bool {
return c.success
}