/
command.go
57 lines (47 loc) · 1.1 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package executor
import (
"context"
"io"
"os"
"os/exec"
"syscall"
"github.com/ErdemOzgen/blackdagger/internal/dag"
)
type CommandExecutor struct {
cmd *exec.Cmd
}
func (e *CommandExecutor) Run() error {
return e.cmd.Run()
}
func (e *CommandExecutor) SetStdout(out io.Writer) {
e.cmd.Stdout = out
}
func (e *CommandExecutor) SetStderr(out io.Writer) {
e.cmd.Stderr = out
}
func (e *CommandExecutor) Kill(sig os.Signal) error {
if e.cmd == nil || e.cmd.Process == nil {
return nil
}
return syscall.Kill(-e.cmd.Process.Pid, sig.(syscall.Signal))
}
func CreateCommandExecutor(ctx context.Context, step *dag.Step) (Executor, error) {
cmd := exec.CommandContext(ctx, step.Command, step.Args...)
cmd.Dir = step.Dir
cmd.Env = append(cmd.Env, step.Variables...)
step.OutputVariables.Range(func(key, value interface{}) bool {
cmd.Env = append(cmd.Env, value.(string))
return true
})
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pgid: 0,
}
return &CommandExecutor{
cmd: cmd,
}, nil
}
func init() {
Register("", CreateCommandExecutor)
Register("command", CreateCommandExecutor)
}