Skip to content

Commit 79d20b1

Browse files
karagencabiosoft
authored andcommitted
Use os.UserHomeDir instead of os.Getenv (#120)
Use user.Current instead of os.Getenv or os.UserHomeDir This way, all operating systems (not only Unix, Windows, macOS, but all of them) are supported
1 parent f2ad97a commit 79d20b1

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

ishell.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io"
1010
"log"
1111
"os"
12+
"os/user"
1213
"path/filepath"
1314
"runtime"
1415
"strings"
@@ -427,10 +428,21 @@ func (s *Shell) SetHistoryPath(path string) {
427428
// SetHomeHistoryPath is a convenience method that sets the history path
428429
// in user's home directory.
429430
func (s *Shell) SetHomeHistoryPath(path string) {
430-
home := os.Getenv("HOME")
431-
if runtime.GOOS == "windows" {
432-
home = os.Getenv("USERPROFILE")
431+
var home string
432+
433+
// Try to get the home directory with user.Current.
434+
// If error occurs, use environment variables
435+
user, err := user.Current()
436+
if err == nil {
437+
home = user.HomeDir
438+
} else {
439+
if runtime.GOOS == "windows" {
440+
home = os.Getenv("USERPROFILE")
441+
} else {
442+
home = os.Getenv("HOME")
443+
}
433444
}
445+
434446
abspath := filepath.Join(home, path)
435447
s.SetHistoryPath(abspath)
436448
}

0 commit comments

Comments
 (0)