blob: 3fdbeb8cf1fef693851d2566ff030bd018e00bd3 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package homedir
18
19import (
20 "os"
21 "path/filepath"
22 "runtime"
23)
24
25// HomeDir returns the home directory for the current user.
26// On Windows:
27// 1. the first of %HOME%, %HOMEDRIVE%%HOMEPATH%, %USERPROFILE% containing a `.kube\config` file is returned.
28// 2. if none of those locations contain a `.kube\config` file, the first of %HOME%, %USERPROFILE%, %HOMEDRIVE%%HOMEPATH% that exists and is writeable is returned.
29// 3. if none of those locations are writeable, the first of %HOME%, %USERPROFILE%, %HOMEDRIVE%%HOMEPATH% that exists is returned.
30// 4. if none of those locations exists, the first of %HOME%, %USERPROFILE%, %HOMEDRIVE%%HOMEPATH% that is set is returned.
31func HomeDir() string {
32 if runtime.GOOS == "windows" {
33 home := os.Getenv("HOME")
34 homeDriveHomePath := ""
35 if homeDrive, homePath := os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH"); len(homeDrive) > 0 && len(homePath) > 0 {
36 homeDriveHomePath = homeDrive + homePath
37 }
38 userProfile := os.Getenv("USERPROFILE")
39
40 // Return first of %HOME%, %HOMEDRIVE%/%HOMEPATH%, %USERPROFILE% that contains a `.kube\config` file.
41 // %HOMEDRIVE%/%HOMEPATH% is preferred over %USERPROFILE% for backwards-compatibility.
42 for _, p := range []string{home, homeDriveHomePath, userProfile} {
43 if len(p) == 0 {
44 continue
45 }
46 if _, err := os.Stat(filepath.Join(p, ".kube", "config")); err != nil {
47 continue
48 }
49 return p
50 }
51
52 firstSetPath := ""
53 firstExistingPath := ""
54
55 // Prefer %USERPROFILE% over %HOMEDRIVE%/%HOMEPATH% for compatibility with other auth-writing tools
56 for _, p := range []string{home, userProfile, homeDriveHomePath} {
57 if len(p) == 0 {
58 continue
59 }
60 if len(firstSetPath) == 0 {
61 // remember the first path that is set
62 firstSetPath = p
63 }
64 info, err := os.Stat(p)
65 if err != nil {
66 continue
67 }
68 if len(firstExistingPath) == 0 {
69 // remember the first path that exists
70 firstExistingPath = p
71 }
72 if info.IsDir() && info.Mode().Perm()&(1<<(uint(7))) != 0 {
73 // return first path that is writeable
74 return p
75 }
76 }
77
78 // If none are writeable, return first location that exists
79 if len(firstExistingPath) > 0 {
80 return firstExistingPath
81 }
82
83 // If none exist, return first location that is set
84 if len(firstSetPath) > 0 {
85 return firstSetPath
86 }
87
88 // We've got nothing
89 return ""
90 }
91 return os.Getenv("HOME")
92}