From 2c44d131503d11839c33ec38daaabbb0f58514d0 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Fri, 6 Feb 2015 10:18:49 -0800 Subject: [PATCH] Export $HOME lookup to pkg/homedir Signed-off-by: Ahmet Alp Balkan --- homedir/MAINTAINERS | 1 + homedir/homedir.go | 16 ++++++++++++++++ homedir/homedir_test.go | 17 +++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 homedir/MAINTAINERS create mode 100644 homedir/homedir.go create mode 100644 homedir/homedir_test.go diff --git a/homedir/MAINTAINERS b/homedir/MAINTAINERS new file mode 100644 index 0000000..82733b8 --- /dev/null +++ b/homedir/MAINTAINERS @@ -0,0 +1 @@ +Ahmet Alp Balkan (@ahmetalpbalkan) diff --git a/homedir/homedir.go b/homedir/homedir.go new file mode 100644 index 0000000..f0246e9 --- /dev/null +++ b/homedir/homedir.go @@ -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") +} diff --git a/homedir/homedir_test.go b/homedir/homedir_test.go new file mode 100644 index 0000000..b89cbf7 --- /dev/null +++ b/homedir/homedir_test.go @@ -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) + } +}