Export $HOME lookup to pkg/homedir
Signed-off-by: Ahmet Alp Balkan <ahmetb@microsoft.com>
This commit is contained in:
parent
193aa34569
commit
2c44d13150
3 changed files with 34 additions and 0 deletions
1
homedir/MAINTAINERS
Normal file
1
homedir/MAINTAINERS
Normal file
|
@ -0,0 +1 @@
|
|||
Ahmet Alp Balkan <ahmetalpbalkan@gmail.com> (@ahmetalpbalkan)
|
16
homedir/homedir.go
Normal file
16
homedir/homedir.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package homedir
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// Get returns the home directory of the current user with the help of
|
||||
// environment variables depending on the target operating system.
|
||||
// Returned path should be used with "path/filepath" to form new paths.
|
||||
func Get() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return os.Getenv("USERPROFILE")
|
||||
}
|
||||
return os.Getenv("HOME")
|
||||
}
|
17
homedir/homedir_test.go
Normal file
17
homedir/homedir_test.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package homedir
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
home := Get()
|
||||
if home == "" {
|
||||
t.Fatal("returned home directory is empty")
|
||||
}
|
||||
|
||||
if !filepath.IsAbs(home) {
|
||||
t.Fatalf("returned path is not absolute: %s", home)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue