From 825745e9e2cce165de9665ae474734ebf4a289a8 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Thu, 2 Jul 2026 20:59:50 -0400 Subject: [PATCH] refactor(consts): lazily initialize cache dir --- internal/consts/consts.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/internal/consts/consts.go b/internal/consts/consts.go index 34583cb..a0680da 100644 --- a/internal/consts/consts.go +++ b/internal/consts/consts.go @@ -4,25 +4,26 @@ import ( "log/slog" "os" "path/filepath" + "sync" ) const Name = "discordo" -var cacheDir string - -func CacheDir() string { - return cacheDir -} - -func init() { +var cacheDir = sync.OnceValue(func() string { userCacheDir, err := os.UserCacheDir() if err != nil { userCacheDir = os.TempDir() slog.Warn("failed to get user cache dir; falling back to temp dir", "err", err, "path", userCacheDir) } - cacheDir = filepath.Join(userCacheDir, Name) - if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil { - slog.Error("failed to create cache dir", "err", err, "path", cacheDir) + path := filepath.Join(userCacheDir, Name) + if err := os.MkdirAll(path, os.ModePerm); err != nil { + slog.Error("failed to create cache dir", "err", err, "path", path) } + + return path +}) + +func CacheDir() string { + return cacheDir() }