aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/mitchellh/go-homedir
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/mitchellh/go-homedir')
-rw-r--r--vendor/github.com/mitchellh/go-homedir/LICENSE21
-rw-r--r--vendor/github.com/mitchellh/go-homedir/README.md14
-rw-r--r--vendor/github.com/mitchellh/go-homedir/homedir.go137
3 files changed, 172 insertions, 0 deletions
diff --git a/vendor/github.com/mitchellh/go-homedir/LICENSE b/vendor/github.com/mitchellh/go-homedir/LICENSE
new file mode 100644
index 0000000..f9c841a
--- /dev/null
+++ b/vendor/github.com/mitchellh/go-homedir/LICENSE
@@ -0,0 +1,21 @@
1The MIT License (MIT)
2
3Copyright (c) 2013 Mitchell Hashimoto
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21THE SOFTWARE.
diff --git a/vendor/github.com/mitchellh/go-homedir/README.md b/vendor/github.com/mitchellh/go-homedir/README.md
new file mode 100644
index 0000000..d70706d
--- /dev/null
+++ b/vendor/github.com/mitchellh/go-homedir/README.md
@@ -0,0 +1,14 @@
1# go-homedir
2
3This is a Go library for detecting the user's home directory without
4the use of cgo, so the library can be used in cross-compilation environments.
5
6Usage is incredibly simple, just call `homedir.Dir()` to get the home directory
7for a user, and `homedir.Expand()` to expand the `~` in a path to the home
8directory.
9
10**Why not just use `os/user`?** The built-in `os/user` package requires
11cgo on Darwin systems. This means that any Go code that uses that package
12cannot cross compile. But 99% of the time the use for `os/user` is just to
13retrieve the home directory, which we can do for the current user without
14cgo. This library does that, enabling cross-compilation.
diff --git a/vendor/github.com/mitchellh/go-homedir/homedir.go b/vendor/github.com/mitchellh/go-homedir/homedir.go
new file mode 100644
index 0000000..47e1f9e
--- /dev/null
+++ b/vendor/github.com/mitchellh/go-homedir/homedir.go
@@ -0,0 +1,137 @@
1package homedir
2
3import (
4 "bytes"
5 "errors"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "runtime"
10 "strconv"
11 "strings"
12 "sync"
13)
14
15// DisableCache will disable caching of the home directory. Caching is enabled
16// by default.
17var DisableCache bool
18
19var homedirCache string
20var cacheLock sync.RWMutex
21
22// Dir returns the home directory for the executing user.
23//
24// This uses an OS-specific method for discovering the home directory.
25// An error is returned if a home directory cannot be detected.
26func Dir() (string, error) {
27 if !DisableCache {
28 cacheLock.RLock()
29 cached := homedirCache
30 cacheLock.RUnlock()
31 if cached != "" {
32 return cached, nil
33 }
34 }
35
36 cacheLock.Lock()
37 defer cacheLock.Unlock()
38
39 var result string
40 var err error
41 if runtime.GOOS == "windows" {
42 result, err = dirWindows()
43 } else {
44 // Unix-like system, so just assume Unix
45 result, err = dirUnix()
46 }
47
48 if err != nil {
49 return "", err
50 }
51 homedirCache = result
52 return result, nil
53}
54
55// Expand expands the path to include the home directory if the path
56// is prefixed with `~`. If it isn't prefixed with `~`, the path is
57// returned as-is.
58func Expand(path string) (string, error) {
59 if len(path) == 0 {
60 return path, nil
61 }
62
63 if path[0] != '~' {
64 return path, nil
65 }
66
67 if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
68 return "", errors.New("cannot expand user-specific home dir")
69 }
70
71 dir, err := Dir()
72 if err != nil {
73 return "", err
74 }
75
76 return filepath.Join(dir, path[1:]), nil
77}
78
79func dirUnix() (string, error) {
80 // First prefer the HOME environmental variable
81 if home := os.Getenv("HOME"); home != "" {
82 return home, nil
83 }
84
85 // If that fails, try getent
86 var stdout bytes.Buffer
87 cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
88 cmd.Stdout = &stdout
89 if err := cmd.Run(); err != nil {
90 // If the error is ErrNotFound, we ignore it. Otherwise, return it.
91 if err != exec.ErrNotFound {
92 return "", err
93 }
94 } else {
95 if passwd := strings.TrimSpace(stdout.String()); passwd != "" {
96 // username:password:uid:gid:gecos:home:shell
97 passwdParts := strings.SplitN(passwd, ":", 7)
98 if len(passwdParts) > 5 {
99 return passwdParts[5], nil
100 }
101 }
102 }
103
104 // If all else fails, try the shell
105 stdout.Reset()
106 cmd = exec.Command("sh", "-c", "cd && pwd")
107 cmd.Stdout = &stdout
108 if err := cmd.Run(); err != nil {
109 return "", err
110 }
111
112 result := strings.TrimSpace(stdout.String())
113 if result == "" {
114 return "", errors.New("blank output when reading home directory")
115 }
116
117 return result, nil
118}
119
120func dirWindows() (string, error) {
121 // First prefer the HOME environmental variable
122 if home := os.Getenv("HOME"); home != "" {
123 return home, nil
124 }
125
126 drive := os.Getenv("HOMEDRIVE")
127 path := os.Getenv("HOMEPATH")
128 home := drive + path
129 if drive == "" || path == "" {
130 home = os.Getenv("USERPROFILE")
131 }
132 if home == "" {
133 return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
134 }
135
136 return home, nil
137}