diff --git a/internal/runner/driver.go b/internal/runner/driver.go index 099a582..c37f627 100644 --- a/internal/runner/driver.go +++ b/internal/runner/driver.go @@ -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 diff --git a/internal/runner/mock_container_driver.go b/internal/runner/mock_container_driver.go index 4f90647..5e52122 100644 --- a/internal/runner/mock_container_driver.go +++ b/internal/runner/mock_container_driver.go @@ -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{ diff --git a/internal/runner/podman_driver.go b/internal/runner/podman_driver.go index a393136..e05381e 100644 --- a/internal/runner/podman_driver.go +++ b/internal/runner/podman_driver.go @@ -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(), diff --git a/internal/runner/runner.go b/internal/runner/runner.go index 5ad2ed8..8e08892 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -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