@@ -4,14 +4,15 @@ import (
4
4
"fmt"
5
5
"io"
6
6
"os"
7
+ "os/user"
7
8
"path/filepath"
9
+ "runtime"
8
10
"strings"
9
11
"sync"
10
12
11
13
"github.com/docker/cli/cli/config/configfile"
12
14
"github.com/docker/cli/cli/config/credentials"
13
15
"github.com/docker/cli/cli/config/types"
14
- "github.com/docker/docker/pkg/homedir"
15
16
"github.com/pkg/errors"
16
17
)
17
18
@@ -42,12 +43,38 @@ func resetConfigDir() {
42
43
initConfigDir = new (sync.Once )
43
44
}
44
45
46
+ // getHomeDir returns the home directory of the current user with the help of
47
+ // environment variables depending on the target operating system.
48
+ // Returned path should be used with "path/filepath" to form new paths.
49
+ //
50
+ // On non-Windows platforms, it falls back to nss lookups, if the home
51
+ // directory cannot be obtained from environment-variables.
52
+ //
53
+ // If linking statically with cgo enabled against glibc, ensure the
54
+ // osusergo build tag is used.
55
+ //
56
+ // If needing to do nss lookups, do not disable cgo or set osusergo.
57
+ //
58
+ // getHomeDir is a copy of [pkg/homedir.Get] to prevent adding docker/docker
59
+ // as dependency for consumers that only need to read the config-file.
60
+ //
61
+ // [pkg/homedir.Get]: https://pkg.go.dev/github.com/docker/docker@v26.1.4+incompatible/pkg/homedir#Get
62
+ func getHomeDir () string {
63
+ home , _ := os .UserHomeDir ()
64
+ if home == "" && runtime .GOOS != "windows" {
65
+ if u , err := user .Current (); err == nil {
66
+ return u .HomeDir
67
+ }
68
+ }
69
+ return home
70
+ }
71
+
45
72
// Dir returns the directory the configuration file is stored in
46
73
func Dir () string {
47
74
initConfigDir .Do (func () {
48
75
configDir = os .Getenv (EnvOverrideConfigDir )
49
76
if configDir == "" {
50
- configDir = filepath .Join (homedir . Get (), configFileDir )
77
+ configDir = filepath .Join (getHomeDir (), configFileDir )
51
78
}
52
79
})
53
80
return configDir
0 commit comments