Skip to content

Commit

Permalink
Add support for podman (#55)
Browse files Browse the repository at this point in the history
## Summary
As requested in #18 this PR adds support for podman via a new `--engine` flag.
  • Loading branch information
loreto committed Sep 2, 2022
1 parent 485e3a4 commit 54bcc89
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions boxcli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func BuildCmd() *cobra.Command {

command.Flags().BoolVar(
&flags.NoCache, "no-cache", false, "Do not use a cache")
command.Flags().StringVar(
&flags.Engine, "engine", "docker", "Engine used to build the container: 'docker', 'podman'")

return command
}
Expand Down
22 changes: 21 additions & 1 deletion docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package docker

import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/imdario/mergo"
"golang.org/x/exp/slices"
)

// This package provides an API to build images using docker.
Expand All @@ -27,6 +29,7 @@ type BuildFlags struct {
Platforms []string
NoCache bool
DockerfilePath string
Engine string
}

type BuildOptions func(*BuildFlags)
Expand All @@ -51,6 +54,10 @@ func Build(path string, opts ...BuildOptions) error {
for _, opt := range opts {
opt(flags)
}
err := validateFlags(flags)
if err != nil {
return err
}

args := []string{"build", "."}
args = ToArgs(args, flags)
Expand All @@ -60,7 +67,12 @@ func Build(path string, opts ...BuildOptions) error {
args = append(args, "-f", fileName)
}

cmd := exec.Command("docker", args...)
binName := "docker"
if flags.Engine != "" {
binName = flags.Engine
}

cmd := exec.Command(binName, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down Expand Up @@ -88,3 +100,11 @@ func isFile(path string) bool {

return fileInfo.Mode().IsRegular()
}

func validateFlags(flags *BuildFlags) error {
engines := []string{"", "docker", "podman"}
if !slices.Contains(engines, flags.Engine) {
return fmt.Errorf("unrecognized container engine: %s", flags.Engine)
}
return nil
}

0 comments on commit 54bcc89

Please sign in to comment.