Skip to content

Commit

Permalink
use user's home directory when fetching the docker config (#2135)
Browse files Browse the repository at this point in the history
  • Loading branch information
hellt committed Jul 9, 2024
1 parent 17758ab commit f0d39b3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
20 changes: 7 additions & 13 deletions runtime/docker/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/distribution/reference"
"github.com/docker/docker/api/types/registry"
log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/utils"
)

const (
Expand Down Expand Up @@ -44,27 +45,20 @@ func getImageDomainName(imageName string) string {
return imageDomainName
}

func getDockerConfigPath(configPath string) (string, error) {
var err error
func getDockerConfigPath(configPath string) string {
if configPath == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}

configPath = filepath.Join(homeDir, dockerDefaultConfigDir, dockerDefaultConfigFile)
configPath = utils.ResolvePath(filepath.Join("~", dockerDefaultConfigDir, dockerDefaultConfigFile), "")
}

return configPath, err
return configPath
}

// GetDockerConfig reads the docker config file by the configPath and returns the DockerConfig struct
// with parts of the docker config.
func GetDockerConfig(configPath string) (*DockerConfig, error) {
var dockerConfig DockerConfig

dockerConfigPath, err := getDockerConfigPath(configPath)
if err != nil {
return nil, err
}
dockerConfigPath := getDockerConfigPath(configPath)

file, err := os.ReadFile(dockerConfigPath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion runtime/docker/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestGetDockerConfigPath(t *testing.T) {
}

for _, in := range td {
got, _ := getDockerConfigPath(in["path"])
got := getDockerConfigPath(in["path"])
want, _ := homedir.Expand(in["want"])
if got != want {
t.Errorf("Invalid docker config path, got %v, want %v", got, in["want"])
Expand Down
2 changes: 2 additions & 0 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ func lookupUserHomeDirViaGetent(userId string) string {

// ResolvePath resolves a string path by expanding `~` to home dir
// or resolving a relative path by joining it with the base path.
// When resolving `~` the function uses the home dir of a sudo user, so that -E sudo
// flag can be omitted.
func ResolvePath(p, base string) string {
if p == "" {
return p
Expand Down

0 comments on commit f0d39b3

Please sign in to comment.