Merge pull request #17658 from NikkeiFTLearning/homeDir-build-in-method

Use built-in method to get user homedir instead of eval on sh
This commit is contained in:
James Bardin 2018-03-23 10:50:42 -04:00 committed by GitHub
commit c8ce63fc16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 11 deletions

View File

@ -3,12 +3,10 @@
package main package main
import ( import (
"bytes"
"errors" "errors"
"os" "os"
"os/exec" "os/user"
"path/filepath" "path/filepath"
"strings"
) )
func configFile() (string, error) { func configFile() (string, error) {
@ -40,18 +38,15 @@ func homeDir() (string, error) {
return home, nil return home, nil
} }
// If that fails, try the shell // If that fails, try build-in module
var stdout bytes.Buffer user, err := user.Current()
cmd := exec.Command("sh", "-c", "eval echo ~$USER") if err != nil {
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", err return "", err
} }
result := strings.TrimSpace(stdout.String()) if user.HomeDir == "" {
if result == "" {
return "", errors.New("blank output") return "", errors.New("blank output")
} }
return result, nil return user.HomeDir, nil
} }