@@ -3,13 +3,14 @@ package context
33import (
44 "net/url"
55 "os"
6+ "os/user"
67 "path/filepath"
8+ "runtime"
79 "strings"
810
911 "github.com/docker/cli/cli/command"
1012 "github.com/docker/cli/cli/context"
1113 "github.com/docker/cli/cli/context/store"
12- "github.com/docker/docker/pkg/homedir"
1314 "k8s.io/client-go/tools/clientcmd"
1415 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
1516)
@@ -99,7 +100,7 @@ func (c *Endpoint) KubernetesConfig() clientcmd.ClientConfig {
99100func (c * EndpointMeta ) ResolveDefault () (any , * store.EndpointTLSData , error ) {
100101 kubeconfig := os .Getenv ("KUBECONFIG" )
101102 if kubeconfig == "" {
102- kubeconfig = filepath .Join (homedir . Get (), ".kube/config" )
103+ kubeconfig = filepath .Join (getHomeDir (), ".kube/config" )
103104 }
104105 kubeEP , err := FromKubeConfig (kubeconfig , "" , "" )
105106 if err != nil {
@@ -156,7 +157,7 @@ func NewKubernetesConfig(configPath string) clientcmd.ClientConfig {
156157 if config := os .Getenv ("KUBECONFIG" ); config != "" {
157158 kubeConfig = config
158159 } else {
159- kubeConfig = filepath .Join (homedir . Get (), ".kube/config" )
160+ kubeConfig = filepath .Join (getHomeDir (), ".kube/config" )
160161 }
161162 }
162163 return clientcmd .NewNonInteractiveDeferredLoadingClientConfig (
@@ -181,3 +182,28 @@ func ConfigFromEndpoint(endpointName string, s store.Reader) (clientcmd.ClientCo
181182 }
182183 return ConfigFromContext (endpointName , s )
183184}
185+
186+ // getHomeDir returns the home directory of the current user with the help of
187+ // environment variables depending on the target operating system.
188+ // Returned path should be used with "path/filepath" to form new paths.
189+ //
190+ // On non-Windows platforms, it falls back to nss lookups, if the home
191+ // directory cannot be obtained from environment-variables.
192+ //
193+ // If linking statically with cgo enabled against glibc, ensure the
194+ // osusergo build tag is used.
195+ //
196+ // If needing to do nss lookups, do not disable cgo or set osusergo.
197+ //
198+ // It's a local fork of [pkg/homedir].
199+ //
200+ // [pkg/homedir]: https://github.com/moby/moby/blob/v28.3.2/pkg/homedir/homedir.go#L9-L28
201+ func getHomeDir () string {
202+ home , _ := os .UserHomeDir ()
203+ if home == "" && runtime .GOOS != "windows" {
204+ if u , err := user .Current (); err == nil {
205+ return u .HomeDir
206+ }
207+ }
208+ return home
209+ }
0 commit comments