homedir.go 2.9 KB

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