refactor: wrap command options as struct to avoid rework when adding new keys
This commit is contained in:
parent
f6a93c0b55
commit
11c964e7be
4 changed files with 23 additions and 9 deletions
|
@ -8,7 +8,7 @@ type ContainerDriver interface {
|
|||
Pull(string) error
|
||||
Start(string, string) error
|
||||
Stop(string) error
|
||||
Exec(containerId string, command string, cwd string, env map[string]string) CommandResult
|
||||
Exec(containerId string, command string, options CommandOptions) CommandResult
|
||||
}
|
||||
|
||||
// Represents the outcome of a command call made by the driver.
|
||||
|
@ -19,6 +19,22 @@ type CommandResult struct {
|
|||
Error error
|
||||
}
|
||||
|
||||
type CommandOptions struct {
|
||||
// Directory to run the command from. Equivalent of cd-ing before running.
|
||||
Cwd string
|
||||
// Map of environment variables exposed in the environment the command runs in.
|
||||
// Note: In the case of container drivers, these mappings are applied within the
|
||||
// container.
|
||||
Env map[string]string
|
||||
}
|
||||
|
||||
func NewCommandOptions() CommandOptions {
|
||||
return CommandOptions{
|
||||
Cwd: ".",
|
||||
Env: map[string]string{},
|
||||
}
|
||||
}
|
||||
|
||||
func NewDriver(driverType string) (ContainerDriver, error) {
|
||||
if driverType == "podman" {
|
||||
return &PodmanDriver{}, nil
|
||||
|
|
|
@ -49,12 +49,12 @@ func (d *MockDriver) Stop(uri string) error {
|
|||
|
||||
}
|
||||
|
||||
func (d *MockDriver) Exec(containerName string, command string, cwd string, env map[string]string) CommandResult {
|
||||
func (d *MockDriver) Exec(containerName string, command string, options CommandOptions) CommandResult {
|
||||
if _, init := d.calls["Exec"]; !init {
|
||||
d.calls["Exec"] = []MockCall{}
|
||||
}
|
||||
|
||||
args := []string{containerName, command, cwd, fmt.Sprintf("%#v", env)}
|
||||
args := []string{containerName, command, options.Cwd, fmt.Sprintf("%#v", options.Env)}
|
||||
d.calls["Exec"] = append(d.calls["Exec"], MockCall{fname: "Exec", args: args})
|
||||
|
||||
mockKeys := []string{
|
||||
|
|
|
@ -79,17 +79,17 @@ func (d PodmanDriver) Stop(containerName string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (d PodmanDriver) Exec(containerId string, command string, cwd string, env map[string]string) CommandResult {
|
||||
func (d PodmanDriver) Exec(containerId string, command string, options CommandOptions) CommandResult {
|
||||
envArgs := []string{}
|
||||
|
||||
for key, value := range env {
|
||||
for key, value := range options.Env {
|
||||
envArgs = append(envArgs, fmt.Sprintf("-e=%s=%s", key, value))
|
||||
}
|
||||
|
||||
commandArgs := []string{
|
||||
"exec",
|
||||
"--workdir",
|
||||
cwd,
|
||||
options.Cwd,
|
||||
}
|
||||
|
||||
commandArgs = append(commandArgs, envArgs...)
|
||||
|
@ -105,8 +105,6 @@ func (d PodmanDriver) Exec(containerId string, command string, cwd string, env m
|
|||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Run()
|
||||
|
||||
fmt.Printf("%#v", cmd)
|
||||
|
||||
return CommandResult{
|
||||
Error: err,
|
||||
ExitCode: cmd.ProcessState.ExitCode(),
|
||||
|
|
|
@ -104,7 +104,7 @@ func (r Runner) runJob(jobContext context.Context, jobTracker *TaskTracker, jobW
|
|||
// If the command raises an error while in the container or fails to run
|
||||
// the command at all, an error is returned, otherwise nil.
|
||||
func (r *Runner) RunCommandInContainer(containerId string, command string, stepCwd string, stepEnv map[string]string) error {
|
||||
result := r.Driver.Exec(containerId, command, stepCwd, stepEnv)
|
||||
result := r.Driver.Exec(containerId, command, CommandOptions{Cwd: stepCwd, Env: stepEnv})
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
|
Loading…
Reference in a new issue