1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Export $HOME lookup to pkg/homedir

Signed-off-by: Ahmet Alp Balkan <ahmetb@microsoft.com>
This commit is contained in:
Ahmet Alp Balkan 2015-02-06 10:18:49 -08:00
parent 6ffb77afd4
commit f9ae2d4fd4
8 changed files with 44 additions and 21 deletions

View file

@ -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)
}

View file

@ -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)

View file

@ -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() {

View file

@ -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
pkg/homedir/MAINTAINERS Normal file
View file

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

16
pkg/homedir/homedir.go Normal file
View file

@ -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")
}

View file

@ -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)
}
}

View file

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