Use build-in method to get user homedir instead of eval on sh
This commit is contained in:
parent
38e6309f03
commit
e4b0a989f3
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue