From 4bf4991307627e8c232551b1df91a0ce9543b523 Mon Sep 17 00:00:00 2001 From: Sergey Avseyev Date: Sat, 24 Feb 2024 21:34:35 -0800 Subject: [PATCH] refactor docker socket detector to detect podman socket now the function uses loop to find working socket among known locations, so that it will be easier to extend it in future --- cmd/init.go | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 5ffac34..a7dfcc6 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -7,7 +7,9 @@ import ( "net" "os" "os/exec" + "os/user" "path" + "path/filepath" "runtime" "strings" "time" @@ -169,22 +171,42 @@ var initCmd = &cobra.Command{ } getDockerDockerHost := func() string { - dockerSocketScheme := "unix" - dockerSocketPath := "/var/run/docker.sock" + type dockerSocketInfo struct { + Scheme string + Path string + } + dockerSockets := []dockerSocketInfo{ + { + Scheme: "unix", + Path: "/var/run/docker.sock", + }, + } + if runtime.GOOS == "windows" { - dockerSocketScheme = "npipe" - dockerSocketPath = "//./pipe/docker_engine" + dockerSockets = append(dockerSockets, dockerSocketInfo{ + Scheme: "npipe", + Path: "//./pipe/docker_engine", + }) + } else if runtime.GOOS == "linux" { + currentUser, err := user.Current() + if err == nil { + dockerSockets = append(dockerSockets, dockerSocketInfo{ + Scheme: "unix", + Path: filepath.Join("/run/user", currentUser.Uid, "podman/podman.sock"), + }) + } } - fmt.Printf("Checking for Docker installation at `%s`... ", dockerSocketPath) - hasDocker := checkFileExists(dockerSocketPath) - if !hasDocker { + for _, socket := range dockerSockets { + fmt.Printf("Checking for Docker installation at `%s`... ", socket.Path) + if checkFileExists(socket.Path) { + fmt.Printf("found.\n") + return socket.Scheme + "://" + socket.Path + } fmt.Printf("not found.\n") - return "" } - fmt.Printf("found.\n") - return dockerSocketScheme + "://" + dockerSocketPath + return "" } getColimaAddress := func() string {