Skip to content

Commit 50acbb0

Browse files
authored
Merge pull request #5187 from thaJeztah/config_no_pkg_homedir
cli/config: replace pkg/homedir dependency with local copy
2 parents 12ea47d + 9f8bda1 commit 50acbb0

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

cli/config/config.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"os/user"
78
"path/filepath"
9+
"runtime"
810
"strings"
911
"sync"
1012

1113
"github.com/docker/cli/cli/config/configfile"
1214
"github.com/docker/cli/cli/config/credentials"
1315
"github.com/docker/cli/cli/config/types"
14-
"github.com/docker/docker/pkg/homedir"
1516
"github.com/pkg/errors"
1617
)
1718

@@ -42,12 +43,38 @@ func resetConfigDir() {
4243
initConfigDir = new(sync.Once)
4344
}
4445

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+
4572
// Dir returns the directory the configuration file is stored in
4673
func Dir() string {
4774
initConfigDir.Do(func() {
4875
configDir = os.Getenv(EnvOverrideConfigDir)
4976
if configDir == "" {
50-
configDir = filepath.Join(homedir.Get(), configFileDir)
77+
configDir = filepath.Join(getHomeDir(), configFileDir)
5178
}
5279
})
5380
return configDir

0 commit comments

Comments
 (0)