Explorar el Código

Export $HOME lookup to pkg/homedir

Signed-off-by: Ahmet Alp Balkan <ahmetb@microsoft.com>
Ahmet Alp Balkan hace 10 años
padre
commit
f9ae2d4fd4

+ 2 - 2
api/client/cli.go

@@ -14,10 +14,10 @@ import (
 	"text/template"
 	"time"
 
+	"github.com/docker/docker/pkg/homedir"
 	flag "github.com/docker/docker/pkg/mflag"
 	"github.com/docker/docker/pkg/term"
 	"github.com/docker/docker/registry"
-	"github.com/docker/docker/utils"
 )
 
 type DockerCli struct {
@@ -105,7 +105,7 @@ func (cli *DockerCli) Subcmd(name, signature, description string, exitOnError bo
 }
 
 func (cli *DockerCli) LoadConfigFile() (err error) {
-	cli.configFile, err = registry.LoadConfig(utils.GetHomeDir())
+	cli.configFile, err = registry.LoadConfig(homedir.Get())
 	if err != nil {
 		fmt.Fprintf(cli.err, "WARNING: %s\n", err)
 	}

+ 2 - 1
api/client/commands.go

@@ -34,6 +34,7 @@ import (
 	"github.com/docker/docker/opts"
 	"github.com/docker/docker/pkg/archive"
 	"github.com/docker/docker/pkg/fileutils"
+	"github.com/docker/docker/pkg/homedir"
 	flag "github.com/docker/docker/pkg/mflag"
 	"github.com/docker/docker/pkg/parsers"
 	"github.com/docker/docker/pkg/parsers/filters"
@@ -388,7 +389,7 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
 	var out2 engine.Env
 	err = out2.Decode(stream)
 	if err != nil {
-		cli.configFile, _ = registry.LoadConfig(utils.GetHomeDir())
+		cli.configFile, _ = registry.LoadConfig(homedir.Get())
 		return err
 	}
 	registry.SaveConfig(cli.configFile)

+ 2 - 1
docker/daemon.go

@@ -16,6 +16,7 @@ import (
 	_ "github.com/docker/docker/daemon/execdriver/native"
 	"github.com/docker/docker/dockerversion"
 	"github.com/docker/docker/engine"
+	"github.com/docker/docker/pkg/homedir"
 	flag "github.com/docker/docker/pkg/mflag"
 	"github.com/docker/docker/pkg/signal"
 	"github.com/docker/docker/registry"
@@ -36,7 +37,7 @@ func init() {
 
 func migrateKey() (err error) {
 	// Migrate trust key if exists at ~/.docker/key.json and owned by current user
-	oldPath := filepath.Join(utils.GetHomeDir(), ".docker", defaultTrustKeyFile)
+	oldPath := filepath.Join(homedir.Get(), ".docker", defaultTrustKeyFile)
 	newPath := filepath.Join(getDaemonConfDir(), defaultTrustKeyFile)
 	if _, statErr := os.Stat(newPath); os.IsNotExist(statErr) && utils.IsFileOwner(oldPath) {
 		defer func() {

+ 4 - 4
docker/flags.go

@@ -7,8 +7,8 @@ import (
 	"runtime"
 
 	"github.com/docker/docker/opts"
+	"github.com/docker/docker/pkg/homedir"
 	flag "github.com/docker/docker/pkg/mflag"
-	"github.com/docker/docker/utils"
 )
 
 var (
@@ -18,14 +18,14 @@ var (
 
 func init() {
 	if dockerCertPath == "" {
-		dockerCertPath = filepath.Join(utils.GetHomeDir(), ".docker")
+		dockerCertPath = filepath.Join(homedir.Get(), ".docker")
 	}
 }
 
 func getDaemonConfDir() string {
 	// TODO: update for Windows daemon
 	if runtime.GOOS == "windows" {
-		return filepath.Join(utils.GetHomeDir(), ".docker")
+		return filepath.Join(homedir.Get(), ".docker")
 	}
 	return "/etc/docker"
 }
@@ -54,7 +54,7 @@ func setDefaultConfFlag(flag *string, def string) {
 		if *flDaemon {
 			*flag = filepath.Join(getDaemonConfDir(), def)
 		} else {
-			*flag = filepath.Join(utils.GetHomeDir(), ".docker", def)
+			*flag = filepath.Join(homedir.Get(), ".docker", def)
 		}
 	}
 }

+ 1 - 0
pkg/homedir/MAINTAINERS

@@ -0,0 +1 @@
+Ahmet Alp Balkan <ahmetalpbalkan@gmail.com> (@ahmetalpbalkan)

+ 16 - 0
pkg/homedir/homedir.go

@@ -0,0 +1,16 @@
+package homedir
+
+import (
+	"os"
+	"runtime"
+)
+
+// Get returns the home directory of the current user with the help of
+// environment variables depending on the target operating system.
+// Returned path should be used with "path/filepath" to form new paths.
+func Get() string {
+	if runtime.GOOS == "windows" {
+		return os.Getenv("USERPROFILE")
+	}
+	return os.Getenv("HOME")
+}

+ 17 - 0
pkg/homedir/homedir_test.go

@@ -0,0 +1,17 @@
+package homedir
+
+import (
+	"path/filepath"
+	"testing"
+)
+
+func TestGet(t *testing.T) {
+	home := Get()
+	if home == "" {
+		t.Fatal("returned home directory is empty")
+	}
+
+	if !filepath.IsAbs(home) {
+		t.Fatalf("returned path is not absolute: %s", home)
+	}
+}

+ 0 - 13
utils/homedir.go

@@ -1,13 +0,0 @@
-package utils
-
-import (
-	"os"
-	"runtime"
-)
-
-func GetHomeDir() string {
-	if runtime.GOOS == "windows" {
-		return os.Getenv("USERPROFILE")
-	}
-	return os.Getenv("HOME")
-}