Skip to content

Commit

Permalink
Added HomeDir function
Browse files Browse the repository at this point in the history
This patch adds the function HomeDir to return the path to the home
directory of the user that owns the current process.
  • Loading branch information
akutz committed Dec 2, 2015
1 parent 9feadf6 commit e82b760
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 17 additions & 2 deletions gotil.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strings"
Expand All @@ -29,8 +30,10 @@ const (
)

var (
trimRx *regexp.Regexp
netAddrRx *regexp.Regexp
homeDir string
homeDirSet bool
trimRx *regexp.Regexp
netAddrRx *regexp.Regexp
)

func init() {
Expand Down Expand Up @@ -265,3 +268,15 @@ func WriteIndentedN(w io.Writer, b []byte, n int) error {
func WriteIndented(w io.Writer, b []byte) error {
return WriteIndentedN(w, b, 4)
}

// HomeDir returns the home directory of the user that owns the current process.
func HomeDir() string {
if homeDirSet {
return homeDir
}
if user, err := user.Current(); err == nil {
homeDir = user.HomeDir
}
homeDirSet = true
return homeDir
}
4 changes: 4 additions & 0 deletions gotil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,7 @@ func TestWriteIndented(t *testing.T) {
}
assert.EqualValues(t, s4, w1.String())
}

func TestHomeDir(t *testing.T) {
assert.NotEqual(t, "", HomeDir())
}

0 comments on commit e82b760

Please sign in to comment.