Extract $HOME usages into utils.GetHomeDir()

Refactored getHomeDir in docker/docker to GetHomeDir in utils
pkg. Currently covers all use cases on the client-side.

Signed-off-by: Ahmet Alp Balkan <ahmetb@microsoft.com>
This commit is contained in:
Ahmet Alp Balkan 2015-01-26 11:04:16 -08:00 committed by Ahmet Alp Balkan
parent abdfb21e3a
commit 6ffb77afd4
5 changed files with 21 additions and 13 deletions

View File

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

View File

@ -388,7 +388,7 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
var out2 engine.Env var out2 engine.Env
err = out2.Decode(stream) err = out2.Decode(stream)
if err != nil { if err != nil {
cli.configFile, _ = registry.LoadConfig(os.Getenv("HOME")) cli.configFile, _ = registry.LoadConfig(utils.GetHomeDir())
return err return err
} }
registry.SaveConfig(cli.configFile) registry.SaveConfig(cli.configFile)

View File

@ -36,7 +36,7 @@ func init() {
func migrateKey() (err error) { func migrateKey() (err error) {
// Migrate trust key if exists at ~/.docker/key.json and owned by current user // Migrate trust key if exists at ~/.docker/key.json and owned by current user
oldPath := filepath.Join(getHomeDir(), ".docker", defaultTrustKeyFile) oldPath := filepath.Join(utils.GetHomeDir(), ".docker", defaultTrustKeyFile)
newPath := filepath.Join(getDaemonConfDir(), defaultTrustKeyFile) newPath := filepath.Join(getDaemonConfDir(), defaultTrustKeyFile)
if _, statErr := os.Stat(newPath); os.IsNotExist(statErr) && utils.IsFileOwner(oldPath) { if _, statErr := os.Stat(newPath); os.IsNotExist(statErr) && utils.IsFileOwner(oldPath) {
defer func() { defer func() {

View File

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

13
utils/homedir.go Normal file
View File

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