From 90836f9a8a8b5d2b1e7fc5fbb87edde88715857d Mon Sep 17 00:00:00 2001
From: Tamer Tas <contact@tmrts.com>
Date: Sat, 28 May 2016 16:27:19 +0300
Subject: [PATCH] Use system calls to get the user home dir

---
 pkg/cmd/bash_completion.go | 19 ++++++++++---------
 pkg/util/osutil/user.go    |  2 +-
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/pkg/cmd/bash_completion.go b/pkg/cmd/bash_completion.go
index ebbd9e1..f425fec 100644
--- a/pkg/cmd/bash_completion.go
+++ b/pkg/cmd/bash_completion.go
@@ -1,7 +1,6 @@
 package cmd
 
 import (
-	"errors"
 	"fmt"
 	"os"
 	"path/filepath"
@@ -9,33 +8,35 @@ import (
 	cli "github.com/spf13/cobra"
 	"github.com/tmrts/boilr/pkg/boilr"
 	"github.com/tmrts/boilr/pkg/util/exit"
+	"github.com/tmrts/boilr/pkg/util/osutil"
 )
 
 func configureBashCompletion() error {
-	bash_completion_file := filepath.Join(boilr.Configuration.ConfigDirPath, "completion.bash")
+	bashCompletionFilePath := filepath.Join(boilr.Configuration.ConfigDirPath, "completion.bash")
 
-	if err := Root.GenBashCompletionFile(bash_completion_file); err != nil {
+	if err := Root.GenBashCompletionFile(bashCompletionFilePath); err != nil {
 		return err
 	}
 
-	if err := Root.GenBashCompletionFile(bash_completion_file); err != nil {
+	if err := Root.GenBashCompletionFile(bashCompletionFilePath); err != nil {
 		return err
 	}
 
-	bashrcPath := filepath.Join(os.Getenv("HOME"), ".bashrc")
-	if bashrcPath == "" {
-		return errors.New("environment variable ${HOME} should be set")
+	homeDir, err := osutil.GetUserHomeDir()
+	if err != nil {
+		return err
 	}
 
+	bashrcPath := filepath.Join(homeDir, ".bashrc")
+
 	f, err := os.OpenFile(bashrcPath, os.O_APPEND|os.O_WRONLY, 0600)
 	if err != nil {
 		return err
 	}
-
 	defer f.Close()
 
 	bashrcText := `
-# Enables shell command completion for boilr
+# Enables command-line completion for boilr
 source %s
 `
 
diff --git a/pkg/util/osutil/user.go b/pkg/util/osutil/user.go
index 4c7be0e..ec7d95f 100644
--- a/pkg/util/osutil/user.go
+++ b/pkg/util/osutil/user.go
@@ -9,5 +9,5 @@ func GetUserHomeDir() (string, error) {
 		return "", err
 	}
 
-	return usr.HomeDir, err
+	return usr.HomeDir, nil
 }
-- 
GitLab