Skip to content

Commit 32c130f

Browse files
committed
refactor: simplify home.Dir()
* Realistically, this should almost never fail. * If it does, returning `""` probably makes more sense than a temp dir. Empty means Go will assume the working directory. * Getting rid of `sync.Once` is good as it locks and this can be called on every render cycle. (Used to compute `~` on the sidebar, etc).
1 parent 1855533 commit 32c130f

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

internal/home/home.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,16 @@
22
package home
33

44
import (
5-
"log/slog"
65
"os"
76
"path/filepath"
87
"strings"
9-
"sync"
108
)
119

12-
// Dir returns the users home directory, or if it fails, tries to create a new
13-
// temporary directory and use that instead.
14-
var Dir = sync.OnceValue(func() string {
15-
home, err := os.UserHomeDir()
16-
if err == nil {
17-
slog.Debug("user home directory", "home", home)
18-
return home
19-
}
20-
tmp, err := os.MkdirTemp("crush", "")
21-
if err != nil {
22-
slog.Error("could not find the user home directory")
23-
return ""
24-
}
25-
slog.Warn("could not find the user home directory, using a temporary one", "home", tmp)
26-
return tmp
27-
})
10+
// Dir returns the user home directory.
11+
func Dir() string {
12+
home, _ := os.UserHomeDir()
13+
return home
14+
}
2815

2916
// Short replaces the actual home path from [Dir] with `~`.
3017
func Short(p string) string {

0 commit comments

Comments
 (0)