Skip to content

Commit

Permalink
Add Docker CLI fallback if Podman is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
dagood committed Feb 28, 2024
1 parent 7ed233d commit c089354
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions cmd/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,8 @@ type Config struct {
}

func (c *Config) Load() error {
envValue := os.Getenv("PODMAN_BIN")
if envValue == "" {
podmanPath, err := exec.LookPath("podman")
if err != nil && errors.Is(err, exec.ErrDot) {
return err
}
if podmanPath != c.PodmanBinary && (podmanPath != "" || len(podmanPath) > 0) {
os.Setenv("PODMAN_BIN", podmanPath)
}
if err := c.loadDefaultPodmanBin(); err != nil {
return err
}

err := env.Set(c)
Expand All @@ -46,3 +39,35 @@ func (c *Config) Load() error {
}
return nil
}

func (c *Config) loadDefaultPodmanBin() error {
// Respect existing PODMAN_BIN setting.
if os.Getenv("PODMAN_BIN") != "" {
return nil
}
// Try to use podman. If it's not found, try to use docker.
found, err := c.trySetDefaultPodmanBin("podman")
if err != nil {
return err
}
if !found {
if _, err = c.trySetDefaultPodmanBin("docker"); err != nil {
return err
}
}
return nil
}

func (c *Config) trySetDefaultPodmanBin(file string) (found bool, err error) {
path, err := exec.LookPath(file)
// Ignore all errors other than ErrDot.
if err != nil && errors.Is(err, exec.ErrDot) {
return false, err
}
// If file was found in PATH and it's not already going to be used, specify it in the env var.
if path != "" && path != c.PodmanBinary {
os.Setenv("PODMAN_BIN", path)
return true, nil
}
return false, nil
}

0 comments on commit c089354

Please sign in to comment.