Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Atrox committed Oct 10, 2017
0 parents commit 06c445c
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
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
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Atrox <mail@atrox.me> (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.
57 changes: 57 additions & 0 deletions README.md
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
45 changes: 45 additions & 0 deletions homedir.go
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
}
89 changes: 89 additions & 0 deletions homedir_test.go
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)
}
}
}

0 comments on commit 06c445c

Please sign in to comment.