From e4b0a989f353a0e9bbca45331c8ae3f9fd69dc50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Socha?= Date: Wed, 21 Mar 2018 14:55:56 +0100 Subject: [PATCH] Use build-in method to get user homedir instead of eval on sh --- config_unix.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/config_unix.go b/config_unix.go index 85c313a02..e8ec1a2aa 100644 --- a/config_unix.go +++ b/config_unix.go @@ -3,12 +3,10 @@ package main import ( - "bytes" "errors" "os" - "os/exec" + "os/user" "path/filepath" - "strings" ) func configFile() (string, error) { @@ -40,18 +38,15 @@ func homeDir() (string, error) { return home, nil } - // If that fails, try the shell - var stdout bytes.Buffer - cmd := exec.Command("sh", "-c", "eval echo ~$USER") - cmd.Stdout = &stdout - if err := cmd.Run(); err != nil { + // If that fails, try build-in module + user, err := user.Current() + if err != nil { return "", err } - result := strings.TrimSpace(stdout.String()) - if result == "" { + if user.HomeDir == "" { return "", errors.New("blank output") } - return result, nil + return user.HomeDir, nil }