implement Executor pattern
This commit is contained in:
11
internal/executor/executor.go
Normal file
11
internal/executor/executor.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type Executor interface {
|
||||
CombinedOutput(context.Context, string, ...string) ([]byte, error)
|
||||
CommandContext(context.Context, string, ...string) *exec.Cmd
|
||||
}
|
||||
16
internal/executor/local.go
Normal file
16
internal/executor/local.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type LocalExecutor struct{}
|
||||
|
||||
func (l LocalExecutor) CombinedOutput(ctx context.Context, name string, arg ...string) ([]byte, error) {
|
||||
return l.CommandContext(ctx, name, arg...).CombinedOutput()
|
||||
}
|
||||
|
||||
func (l LocalExecutor) CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
|
||||
return exec.CommandContext(ctx, name, arg...)
|
||||
}
|
||||
21
internal/executor/ssh.go
Normal file
21
internal/executor/ssh.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type SSHExecutor struct {
|
||||
SSHTarget string
|
||||
}
|
||||
|
||||
func (s SSHExecutor) CombinedOutput(ctx context.Context, name string, arg ...string) ([]byte, error) {
|
||||
return s.CommandContext(ctx, name, arg...).CombinedOutput()
|
||||
}
|
||||
|
||||
func (s SSHExecutor) CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
|
||||
newArg := append([]string{s.SSHTarget, name}, arg...)
|
||||
cmd := exec.CommandContext(ctx, "ssh", newArg...)
|
||||
|
||||
return cmd
|
||||
}
|
||||
Reference in New Issue
Block a user