This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from tyrken/windows_homedir_expansion
Fix Windows inability to handle a tilde as the home directory
- Loading branch information
Showing
5 changed files
with
265 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013 Mitchell Hashimoto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# go-homedir | ||
|
||
This is a Go library for detecting the user's home directory without | ||
the use of cgo, so the library can be used in cross-compilation environments. | ||
|
||
Usage is incredibly simple, just call `homedir.Dir()` to get the home directory | ||
for a user, and `homedir.Expand()` to expand the `~` in a path to the home | ||
directory. | ||
|
||
**Why not just use `os/user`?** The built-in `os/user` package requires | ||
cgo on Darwin systems. This means that any Go code that uses that package | ||
cannot cross compile. But 99% of the time the use for `os/user` is just to | ||
retrieve the home directory, which we can do for the current user without | ||
cgo. This library does that, enabling cross-compilation. |
112 changes: 112 additions & 0 deletions
112
vendor/src/github.com/mitchellh/go-homedir/homedir.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package homedir | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
// DisableCache will disable caching of the home directory. Caching is enabled | ||
// by default. | ||
var DisableCache bool | ||
|
||
var homedirCache string | ||
var cacheLock sync.RWMutex | ||
|
||
// Dir returns the home directory for the executing user. | ||
// | ||
// This uses an OS-specific method for discovering the home directory. | ||
// An error is returned if a home directory cannot be detected. | ||
func Dir() (string, error) { | ||
if !DisableCache { | ||
cacheLock.RLock() | ||
cached := homedirCache | ||
cacheLock.RUnlock() | ||
if cached != "" { | ||
return cached, nil | ||
} | ||
} | ||
|
||
cacheLock.Lock() | ||
defer cacheLock.Unlock() | ||
|
||
var result string | ||
var err error | ||
if runtime.GOOS == "windows" { | ||
result, err = dirWindows() | ||
} else { | ||
// Unix-like system, so just assume Unix | ||
result, err = dirUnix() | ||
} | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
homedirCache = result | ||
return result, nil | ||
} | ||
|
||
// Expand expands the path to include the home directory if the path | ||
// is prefixed with `~`. If it isn't prefixed with `~`, the path is | ||
// returned as-is. | ||
func Expand(path string) (string, error) { | ||
if len(path) == 0 { | ||
return path, nil | ||
} | ||
|
||
if path[0] != '~' { | ||
return path, nil | ||
} | ||
|
||
if len(path) > 1 && path[1] != '/' && path[1] != '\\' { | ||
return "", errors.New("cannot expand user-specific home dir") | ||
} | ||
|
||
dir, err := Dir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return filepath.Join(dir, path[1:]), nil | ||
} | ||
|
||
func dirUnix() (string, error) { | ||
// First prefer the HOME environmental variable | ||
if home := os.Getenv("HOME"); home != "" { | ||
return home, nil | ||
} | ||
|
||
// If that fails, try the shell | ||
var stdout bytes.Buffer | ||
cmd := exec.Command("sh", "-c", "eval echo ~$USER") | ||
cmd.Stdout = &stdout | ||
if err := cmd.Run(); err != nil { | ||
return "", err | ||
} | ||
|
||
result := strings.TrimSpace(stdout.String()) | ||
if result == "" { | ||
return "", errors.New("blank output when reading home directory") | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func dirWindows() (string, error) { | ||
drive := os.Getenv("HOMEDRIVE") | ||
path := os.Getenv("HOMEPATH") | ||
home := drive + path | ||
if drive == "" || path == "" { | ||
home = os.Getenv("USERPROFILE") | ||
} | ||
if home == "" { | ||
return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank") | ||
} | ||
|
||
return home, nil | ||
} |
112 changes: 112 additions & 0 deletions
112
vendor/src/github.com/mitchellh/go-homedir/homedir_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package homedir | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/user" | ||
"testing" | ||
) | ||
|
||
func patchEnv(key, value string) func() { | ||
bck := os.Getenv(key) | ||
deferFunc := func() { | ||
os.Setenv(key, bck) | ||
} | ||
|
||
os.Setenv(key, value) | ||
return deferFunc | ||
} | ||
|
||
func BenchmarkDir(b *testing.B) { | ||
// We do this for any "warmups" | ||
for i := 0; i < 10; i++ { | ||
Dir() | ||
} | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
Dir() | ||
} | ||
} | ||
|
||
func TestDir(t *testing.T) { | ||
u, err := user.Current() | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
dir, err := Dir() | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
if u.HomeDir != dir { | ||
t.Fatalf("%#v != %#v", u.HomeDir, dir) | ||
} | ||
} | ||
|
||
func TestExpand(t *testing.T) { | ||
u, err := user.Current() | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
cases := []struct { | ||
Input string | ||
Output string | ||
Err bool | ||
}{ | ||
{ | ||
"/foo", | ||
"/foo", | ||
false, | ||
}, | ||
|
||
{ | ||
"~/foo", | ||
fmt.Sprintf("%s/foo", u.HomeDir), | ||
false, | ||
}, | ||
|
||
{ | ||
"", | ||
"", | ||
false, | ||
}, | ||
|
||
{ | ||
"~", | ||
u.HomeDir, | ||
false, | ||
}, | ||
|
||
{ | ||
"~foo/foo", | ||
"", | ||
true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
actual, err := Expand(tc.Input) | ||
if (err != nil) != tc.Err { | ||
t.Fatalf("Input: %#v\n\nErr: %s", tc.Input, err) | ||
} | ||
|
||
if actual != tc.Output { | ||
t.Fatalf("Input: %#v\n\nOutput: %#v", tc.Input, actual) | ||
} | ||
} | ||
|
||
DisableCache = true | ||
defer func() { DisableCache = false }() | ||
defer patchEnv("HOME", "/custom/path/")() | ||
expected := "/custom/path/foo/bar" | ||
actual, err := Expand("~/foo/bar") | ||
|
||
if err != nil { | ||
t.Errorf("No error is expected, got: %v", err) | ||
} else if actual != "/custom/path/foo/bar" { | ||
t.Errorf("Expected: %v; actual: %v", expected, actual) | ||
} | ||
} |