Browse code

Use correct homedir on Windows

Fabiano Franz authored on 2015/11/10 03:45:12
Showing 2 changed files
... ...
@@ -465,7 +465,7 @@ func MakeRelative(path, base string) (string, error) {
465 465
 	return path, nil
466 466
 }
467 467
 
468
-// HomeDir return the home directory for the current user
468
+// HomeDir returns the home directory for the current user
469 469
 func HomeDir() string {
470 470
 	if runtime.GOOS == "windows" {
471 471
 		if homeDrive, homePath := os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH"); len(homeDrive) > 0 && len(homePath) > 0 {
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"os"
5 5
 	"path"
6 6
 	"path/filepath"
7
+	"runtime"
7 8
 
8 9
 	"github.com/spf13/cobra"
9 10
 
... ...
@@ -21,27 +22,39 @@ const (
21 21
 	OpenShiftConfigHomeDirFileName = OpenShiftConfigHomeDir + "/" + OpenShiftConfigHomeFileName
22 22
 )
23 23
 
24
-var OldRecommendedHomeFile = path.Join(os.Getenv("HOME"), ".kube/.config")
25
-var RecommendedHomeFile = path.Join(os.Getenv("HOME"), OpenShiftConfigHomeDirFileName)
24
+var RecommendedHomeFile = path.Join(kclientcmd.HomeDir(), OpenShiftConfigHomeDirFileName)
25
+
26
+// currentMigrationRules returns a map that holds the history of recommended home directories used in previous versions.
27
+// Any future changes to RecommendedHomeFile and related are expected to add a migration rule here, in order to make
28
+// sure existing config files are migrated to their new locations properly.
29
+func currentMigrationRules() map[string]string {
30
+	oldRecommendedHomeFile := path.Join(kclientcmd.HomeDir(), ".kube/.config")
31
+	oldRecommendedWindowsHomeFile := path.Join(os.Getenv("HOME"), OpenShiftConfigHomeDirFileName)
32
+
33
+	migrationRules := map[string]string{}
34
+	migrationRules[RecommendedHomeFile] = oldRecommendedHomeFile
35
+	if runtime.GOOS == "windows" {
36
+		migrationRules[RecommendedHomeFile] = oldRecommendedWindowsHomeFile
37
+	}
38
+	return migrationRules
39
+}
26 40
 
27 41
 // NewOpenShiftClientConfigLoadingRules returns file priority loading rules for OpenShift.
28 42
 // 1. --config value
29 43
 // 2. if KUBECONFIG env var has a value, use it. Otherwise, ~/.kube/config file
30 44
 func NewOpenShiftClientConfigLoadingRules() *clientcmd.ClientConfigLoadingRules {
31 45
 	chain := []string{}
32
-	migrationRules := map[string]string{}
33 46
 
34 47
 	envVarFile := os.Getenv(OpenShiftConfigPathEnvVar)
35 48
 	if len(envVarFile) != 0 {
36 49
 		chain = append(chain, filepath.SplitList(envVarFile)...)
37 50
 	} else {
38 51
 		chain = append(chain, RecommendedHomeFile)
39
-		migrationRules[RecommendedHomeFile] = OldRecommendedHomeFile
40 52
 	}
41 53
 
42 54
 	return &clientcmd.ClientConfigLoadingRules{
43 55
 		Precedence:     chain,
44
-		MigrationRules: migrationRules,
56
+		MigrationRules: currentMigrationRules(),
45 57
 	}
46 58
 }
47 59