Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Docker CLI fallback if Podman is not found #165

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading