Skip to content

Commit 54bcc89

Browse files
authored
Add support for podman (#55)
## Summary As requested in #18 this PR adds support for podman via a new `--engine` flag.
1 parent 485e3a4 commit 54bcc89

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

boxcli/build.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func BuildCmd() *cobra.Command {
2222

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

2628
return command
2729
}

docker/docker.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
package docker
55

66
import (
7+
"fmt"
78
"os"
89
"os/exec"
910
"path/filepath"
1011

1112
"github.com/imdario/mergo"
13+
"golang.org/x/exp/slices"
1214
)
1315

1416
// This package provides an API to build images using docker.
@@ -27,6 +29,7 @@ type BuildFlags struct {
2729
Platforms []string
2830
NoCache bool
2931
DockerfilePath string
32+
Engine string
3033
}
3134

3235
type BuildOptions func(*BuildFlags)
@@ -51,6 +54,10 @@ func Build(path string, opts ...BuildOptions) error {
5154
for _, opt := range opts {
5255
opt(flags)
5356
}
57+
err := validateFlags(flags)
58+
if err != nil {
59+
return err
60+
}
5461

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

63-
cmd := exec.Command("docker", args...)
70+
binName := "docker"
71+
if flags.Engine != "" {
72+
binName = flags.Engine
73+
}
74+
75+
cmd := exec.Command(binName, args...)
6476
cmd.Stdin = os.Stdin
6577
cmd.Stdout = os.Stdout
6678
cmd.Stderr = os.Stderr
@@ -88,3 +100,11 @@ func isFile(path string) bool {
88100

89101
return fileInfo.Mode().IsRegular()
90102
}
103+
104+
func validateFlags(flags *BuildFlags) error {
105+
engines := []string{"", "docker", "podman"}
106+
if !slices.Contains(engines, flags.Engine) {
107+
return fmt.Errorf("unrecognized container engine: %s", flags.Engine)
108+
}
109+
return nil
110+
}

0 commit comments

Comments
 (0)