From 23eef5de24efad787dcd9cb4773514d66c7585a2 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Mon, 2 Sep 2024 00:46:21 -0400 Subject: [PATCH] refactor: tidy podman driver, remove redundant command setup --- internal/driver/podman_driver.go | 52 +++++++++++--------------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/internal/driver/podman_driver.go b/internal/driver/podman_driver.go index b57da3e..9a57f58 100644 --- a/internal/driver/podman_driver.go +++ b/internal/driver/podman_driver.go @@ -10,18 +10,21 @@ import ( "os/exec" ) -type PodmanDriver struct{} - -func (d PodmanDriver) Pull(uri string) error { - cmd := exec.Command("podman", "pull", uri) +// Executes a Podman command and pipes output to stdout/stderr. +// +// The exec.Cmd pointer returned isn't ran. +func podmanCommand(args ...string) *exec.Cmd { + cmd := exec.Command("podman", args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return err - } + return cmd +} - return nil +type PodmanDriver struct{} + +func (d PodmanDriver) Pull(uri string) error { + return podmanCommand("pull", uri).Run() } // Sets up a new container with an attached workspace volume. @@ -30,19 +33,12 @@ func (d PodmanDriver) Pull(uri string) error { // can be easily collected and cleaned up on stop. func (d PodmanDriver) Start(uri string, containerName string) error { volumeName := fmt.Sprintf("%s-workspace", containerName) - cmd := exec.Command("podman", "volume", "create", volumeName, "--label", fmt.Sprintf("owner=%s", containerName)) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { + if err := podmanCommand("volume", "create", volumeName, "--label", fmt.Sprintf("owner=%s", containerName)).Run(); err != nil { return err } - cmd = exec.Command("podman", "run", "-td", "--name", containerName, "-v", fmt.Sprintf("%s:/workspace", volumeName), uri) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if err := cmd.Run(); err != nil { + if err := podmanCommand("run", "-td", "--name", containerName, "-v", fmt.Sprintf("%s:/workspace", volumeName), uri).Run(); err != nil { return err } @@ -52,27 +48,15 @@ func (d PodmanDriver) Start(uri string, containerName string) error { // Stops a container and removes any volumes labelled with its name // as owner. func (d PodmanDriver) Stop(containerName string) error { - cmd := exec.Command("podman", "rm", "-f", "-t", "2", containerName) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if err := cmd.Run(); err != nil { + if err := podmanCommand("rm", "-f", "-t", "2", containerName).Run(); err != nil { return err } - cmd = exec.Command("podman", "wait", containerName, "--ignore") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if err := cmd.Run(); err != nil { + if err := podmanCommand("wait", containerName, "--ignore").Run(); err != nil { logger.Error("%+v", err) } - cmd = exec.Command("podman", "volume", "prune", "--filter", fmt.Sprintf("label=owner=%s", containerName), "-f") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if err := cmd.Run(); err != nil { + if err := podmanCommand("volume", "prune", "--filter", fmt.Sprintf("label=owner=%s", containerName), "-f").Run(); err != nil { return err } @@ -100,9 +84,7 @@ func (d PodmanDriver) Exec(containerId string, command string, options CommandOp command, ) - cmd := exec.Command("podman", commandArgs...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd := podmanCommand(commandArgs...) err := cmd.Run() return CommandResult{