Browse code

Export $HOME lookup to pkg/homedir

Signed-off-by: Ahmet Alp Balkan <ahmetb@microsoft.com>

Ahmet Alp Balkan authored on 2015/02/07 03:18:49
Showing 8 changed files
... ...
@@ -14,10 +14,10 @@ import (
14 14
 	"text/template"
15 15
 	"time"
16 16
 
17
+	"github.com/docker/docker/pkg/homedir"
17 18
 	flag "github.com/docker/docker/pkg/mflag"
18 19
 	"github.com/docker/docker/pkg/term"
19 20
 	"github.com/docker/docker/registry"
20
-	"github.com/docker/docker/utils"
21 21
 )
22 22
 
23 23
 type DockerCli struct {
... ...
@@ -105,7 +105,7 @@ func (cli *DockerCli) Subcmd(name, signature, description string, exitOnError bo
105 105
 }
106 106
 
107 107
 func (cli *DockerCli) LoadConfigFile() (err error) {
108
-	cli.configFile, err = registry.LoadConfig(utils.GetHomeDir())
108
+	cli.configFile, err = registry.LoadConfig(homedir.Get())
109 109
 	if err != nil {
110 110
 		fmt.Fprintf(cli.err, "WARNING: %s\n", err)
111 111
 	}
... ...
@@ -34,6 +34,7 @@ import (
34 34
 	"github.com/docker/docker/opts"
35 35
 	"github.com/docker/docker/pkg/archive"
36 36
 	"github.com/docker/docker/pkg/fileutils"
37
+	"github.com/docker/docker/pkg/homedir"
37 38
 	flag "github.com/docker/docker/pkg/mflag"
38 39
 	"github.com/docker/docker/pkg/parsers"
39 40
 	"github.com/docker/docker/pkg/parsers/filters"
... ...
@@ -388,7 +389,7 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
388 388
 	var out2 engine.Env
389 389
 	err = out2.Decode(stream)
390 390
 	if err != nil {
391
-		cli.configFile, _ = registry.LoadConfig(utils.GetHomeDir())
391
+		cli.configFile, _ = registry.LoadConfig(homedir.Get())
392 392
 		return err
393 393
 	}
394 394
 	registry.SaveConfig(cli.configFile)
... ...
@@ -16,6 +16,7 @@ import (
16 16
 	_ "github.com/docker/docker/daemon/execdriver/native"
17 17
 	"github.com/docker/docker/dockerversion"
18 18
 	"github.com/docker/docker/engine"
19
+	"github.com/docker/docker/pkg/homedir"
19 20
 	flag "github.com/docker/docker/pkg/mflag"
20 21
 	"github.com/docker/docker/pkg/signal"
21 22
 	"github.com/docker/docker/registry"
... ...
@@ -36,7 +37,7 @@ func init() {
36 36
 
37 37
 func migrateKey() (err error) {
38 38
 	// Migrate trust key if exists at ~/.docker/key.json and owned by current user
39
-	oldPath := filepath.Join(utils.GetHomeDir(), ".docker", defaultTrustKeyFile)
39
+	oldPath := filepath.Join(homedir.Get(), ".docker", defaultTrustKeyFile)
40 40
 	newPath := filepath.Join(getDaemonConfDir(), defaultTrustKeyFile)
41 41
 	if _, statErr := os.Stat(newPath); os.IsNotExist(statErr) && utils.IsFileOwner(oldPath) {
42 42
 		defer func() {
... ...
@@ -7,8 +7,8 @@ import (
7 7
 	"runtime"
8 8
 
9 9
 	"github.com/docker/docker/opts"
10
+	"github.com/docker/docker/pkg/homedir"
10 11
 	flag "github.com/docker/docker/pkg/mflag"
11
-	"github.com/docker/docker/utils"
12 12
 )
13 13
 
14 14
 var (
... ...
@@ -18,14 +18,14 @@ var (
18 18
 
19 19
 func init() {
20 20
 	if dockerCertPath == "" {
21
-		dockerCertPath = filepath.Join(utils.GetHomeDir(), ".docker")
21
+		dockerCertPath = filepath.Join(homedir.Get(), ".docker")
22 22
 	}
23 23
 }
24 24
 
25 25
 func getDaemonConfDir() string {
26 26
 	// TODO: update for Windows daemon
27 27
 	if runtime.GOOS == "windows" {
28
-		return filepath.Join(utils.GetHomeDir(), ".docker")
28
+		return filepath.Join(homedir.Get(), ".docker")
29 29
 	}
30 30
 	return "/etc/docker"
31 31
 }
... ...
@@ -54,7 +54,7 @@ func setDefaultConfFlag(flag *string, def string) {
54 54
 		if *flDaemon {
55 55
 			*flag = filepath.Join(getDaemonConfDir(), def)
56 56
 		} else {
57
-			*flag = filepath.Join(utils.GetHomeDir(), ".docker", def)
57
+			*flag = filepath.Join(homedir.Get(), ".docker", def)
58 58
 		}
59 59
 	}
60 60
 }
61 61
new file mode 100644
... ...
@@ -0,0 +1 @@
0
+Ahmet Alp Balkan <ahmetalpbalkan@gmail.com> (@ahmetalpbalkan)
0 1
new file mode 100644
... ...
@@ -0,0 +1,16 @@
0
+package homedir
1
+
2
+import (
3
+	"os"
4
+	"runtime"
5
+)
6
+
7
+// Get returns the home directory of the current user with the help of
8
+// environment variables depending on the target operating system.
9
+// Returned path should be used with "path/filepath" to form new paths.
10
+func Get() string {
11
+	if runtime.GOOS == "windows" {
12
+		return os.Getenv("USERPROFILE")
13
+	}
14
+	return os.Getenv("HOME")
15
+}
0 16
new file mode 100644
... ...
@@ -0,0 +1,17 @@
0
+package homedir
1
+
2
+import (
3
+	"path/filepath"
4
+	"testing"
5
+)
6
+
7
+func TestGet(t *testing.T) {
8
+	home := Get()
9
+	if home == "" {
10
+		t.Fatal("returned home directory is empty")
11
+	}
12
+
13
+	if !filepath.IsAbs(home) {
14
+		t.Fatalf("returned path is not absolute: %s", home)
15
+	}
16
+}
0 17
deleted file mode 100644
... ...
@@ -1,13 +0,0 @@
1
-package utils
2
-
3
-import (
4
-	"os"
5
-	"runtime"
6
-)
7
-
8
-func GetHomeDir() string {
9
-	if runtime.GOOS == "windows" {
10
-		return os.Getenv("USERPROFILE")
11
-	}
12
-	return os.Getenv("HOME")
13
-}