-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 06c445c
Showing
5 changed files
with
224 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
language: go | ||
|
||
go: | ||
- 1.8 | ||
- 1.9 | ||
- tip | ||
|
||
before_install: | ||
- go get github.com/mattn/goveralls | ||
|
||
script: | ||
- $HOME/gopath/bin/goveralls -service=travis-ci |
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 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Atrox <[email protected]> (https://atrox.me) | ||
|
||
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,57 @@ | ||
# HomeDir | ||
|
||
[![Build Status](https://img.shields.io/travis/Atrox/homedir.svg?style=flat-square)](https://travis-ci.org/Atrox/homedir) | ||
[![Coverage Status](https://img.shields.io/coveralls/Atrox/homedir.svg?style=flat-square)](https://coveralls.io/r/Atrox/homedir) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/atrox/homedir?style=flat-square)](https://goreportcard.com/report/github.com/atrox/homedir) | ||
[![GoDoc](https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://godoc.org/github.com/Atrox/homedir) | ||
|
||
> This library is based on and fully compatible with [mitchellh/go-homedir](https://github.com/mitchellh/go-homedir) but uses `os/user` because since go 1.9 there is no longer cgo compilation required. | ||
## Installation | ||
|
||
```sh | ||
go get -u github.com/atrox/homedir | ||
# or with dep | ||
dep ensure -add github.com/atrox/homedir | ||
``` | ||
|
||
## Usage | ||
|
||
Usage is incredibly simple, just call `homedir.Dir()` to get the home directory | ||
for a user, and `homedir.Expand(path string)` to expand the `~` in a path to the home | ||
directory. | ||
|
||
## Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/atrox/homedir" | ||
) | ||
|
||
func main() { | ||
dir, err := homedir.Dir() | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("'%s' is your users home directory\n", dir) | ||
|
||
path, err := homedir.Expand("~/.config") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("'%s' is the expanded path to the .config directory\n", path) | ||
} | ||
``` | ||
|
||
## Contributing | ||
|
||
Everyone is encouraged to help improve this project. Here are a few ways you can help: | ||
|
||
- [Report bugs](https://github.com/atrox/homedir/issues) | ||
- Fix bugs and [submit pull requests](https://github.com/atrox/homedir/pulls) | ||
- Write, clarify, or fix documentation | ||
- Suggest or add new features |
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,45 @@ | ||
package homedir | ||
|
||
import ( | ||
"errors" | ||
"os/user" | ||
"path/filepath" | ||
) | ||
|
||
// Dir returns the home directory for the executing user. | ||
// An error is returned if a home directory cannot be detected. | ||
func Dir() (string, error) { | ||
currentUser, err := user.Current() | ||
if err != nil { | ||
return "", err | ||
} | ||
if currentUser.HomeDir == "" { | ||
return "", errors.New("cannot find user-specific home dir") | ||
} | ||
|
||
return currentUser.HomeDir, 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 | ||
} |
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,89 @@ | ||
package homedir | ||
|
||
import ( | ||
"os/user" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
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", | ||
filepath.Join(u.HomeDir, "foo"), | ||
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) | ||
} | ||
} | ||
} |