From ee15c01ffbc58cc202580ab5750d437fc737e135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=88=D2=88=D2=88Luiz=20Branco?= Date: Wed, 15 Nov 2017 16:55:33 -0400 Subject: [PATCH] Check env HOME before calling user.Current() (#179) --- CHANGELOG.md | 6 ++++++ config/config.go | 26 +++++++++++++++++++------- plugins/plugins.go | 13 ++----------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 548250b..f8e88c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +## [0.10.1] - 2017-11-15 + +### Fixed + +- `os/user.Current` when cgo is disabled + ## [0.10.0] - 2017-11-03 ### Added diff --git a/config/config.go b/config/config.go index f467696..3f200f2 100644 --- a/config/config.go +++ b/config/config.go @@ -135,16 +135,11 @@ func checkPermissions(path string) error { // RCPath returns the absolute path to the ~/.manifoldrc file func RCPath() (string, error) { - u, err := user.Current() + home, err := UserHome() if err != nil { return "", err } - - if u.HomeDir == "" { - return "", ErrMissingHomeDir - } - - return path.Join(u.HomeDir, rcFilename), nil + return path.Join(home, rcFilename), nil } // Config represents the configuration which is stored inside a ~/.manifoldrc @@ -334,3 +329,20 @@ func isSystemRoot(path string) bool { return os.PathSeparator == path[rootPathLength-1] } + +func UserHome() (string, error) { + home := os.Getenv("HOME") + if home != "" { + return home, nil + } + u, err := user.Current() + if err != nil { + return "", err + } + + if u.HomeDir == "" { + return "", ErrMissingHomeDir + } + + return u.HomeDir, nil +} diff --git a/plugins/plugins.go b/plugins/plugins.go index b0e24c1..0490788 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -7,7 +7,6 @@ import ( "net/url" "os" "os/exec" - "os/user" "path" "runtime" "strings" @@ -27,24 +26,16 @@ const ( // ErrFailedToRead is returned when you the plugins directory cannot be read var ErrFailedToRead = errors.New("Failed to read plugins directory") -// ErrMissingHomeDir represents an error when a home directory could not be found -var ErrMissingHomeDir = errors.New("Could not find Home Directory") - // ErrBadPluginRepository represents an error when the plugin url is invalidu var ErrBadPluginRepository = errors.New("Invalid repository URL") // Path returns the plugin directory's path func Path() (string, error) { - u, err := user.Current() + home, err := config.UserHome() if err != nil { return "", err } - - if u.HomeDir == "" { - return "", ErrMissingHomeDir - } - - return path.Join(u.HomeDir, directory), nil + return path.Join(home, directory), nil } // Executable returns the executable path