diff --git a/container.go b/container.go index c55c5ac..1b47997 100644 --- a/container.go +++ b/container.go @@ -20,6 +20,8 @@ import ( "code.dumpstack.io/tools/out-of-tree/config" ) +var containerRuntime = "docker" + type ContainerCmd struct { Filter string `help:"filter by name"` @@ -56,7 +58,8 @@ type ContainerCleanupCmd struct{} func (cmd ContainerCleanupCmd) Run(containerCmd *ContainerCmd) (err error) { var output []byte for _, name := range containerCmd.Containers() { - output, err = exec.Command("docker", "image", "rm", name).CombinedOutput() + output, err = exec.Command(containerRuntime, "image", "rm", name). + CombinedOutput() if err != nil { log.Error().Err(err).Str("output", string(output)).Msg("") return @@ -72,7 +75,7 @@ type containerImageInfo struct { } func listContainerImages() (diis []containerImageInfo, err error) { - cmd := exec.Command("docker", "images") + cmd := exec.Command(containerRuntime, "images") log.Debug().Msgf("%v", cmd) rawOutput, err := cmd.CombinedOutput() @@ -148,7 +151,7 @@ func (c container) Build(imagePath string) (output string, err error) { args := []string{"build"} args = append(args, "-t", c.name, imagePath) - cmd := exec.Command("docker", args...) + cmd := exec.Command(containerRuntime, args...) flog := log.With(). Str("command", fmt.Sprintf("%v", cmd)). @@ -207,7 +210,7 @@ func (c container) Run(workdir string, command string) (output string, err error args = append(args, command) } - cmd := exec.Command("docker", args...) + cmd := exec.Command(containerRuntime, args...) log.Debug().Msgf("%v", cmd) diff --git a/kernel.go b/kernel.go index 5e904e8..3124cb3 100644 --- a/kernel.go +++ b/kernel.go @@ -262,7 +262,8 @@ func generateBaseDockerImage(registry string, commands []config.DockerCommand, d := "# BASE\n" - cmd := exec.Command("docker", "images", "-q", sk.DockerName()) + // TODO move as function to container.go + cmd := exec.Command(containerRuntime, "images", "-q", sk.DockerName()) log.Debug().Msgf("run %v", cmd) rawOutput, err := cmd.CombinedOutput() diff --git a/main.go b/main.go index f19754f..bcf3ebe 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "io" "math/rand" "os" + "os/exec" "os/user" "runtime/debug" "strconv" @@ -44,6 +45,8 @@ type CLI struct { Version VersionFlag `name:"version" help:"print version information and quit"` LogLevel LogLevelFlag `enum:"trace,debug,info,warn,error" default:"info"` + + ContainerRuntime string `enum:"podman,docker" default:"podman"` } type LogLevelFlag string @@ -153,6 +156,22 @@ func main() { log.Debug().Msgf("%v", buildInfo.Settings) } + _, err = exec.LookPath(cli.ContainerRuntime) + if err != nil { + if cli.ContainerRuntime == "podman" { // default value + log.Debug().Msgf("podman is not found in $PATH, " + + "fall back to docker") + cli.ContainerRuntime = "docker" + } + + _, err = exec.LookPath(cli.ContainerRuntime) + if err != nil { + log.Fatal().Msgf("%v is not found in $PATH", + cli.ContainerRuntime) + } + } + containerRuntime = cli.ContainerRuntime + err = ctx.Run(&cli.Globals) ctx.FatalIfErrorf(err) }