From 172df744f4ebb1b5199839c670f52803d18ee426 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Tue, 19 Aug 2025 15:05:17 -0300 Subject: [PATCH] refactor: move `HomeDir()` to `fsext` package --- internal/config/load.go | 15 -------------- internal/fsext/home.go | 20 +++++++++++++++++++ internal/fsext/ls.go | 3 +-- .../tui/components/dialogs/models/apikey.go | 3 ++- 4 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 internal/fsext/home.go diff --git a/internal/config/load.go b/internal/config/load.go index 12defe528334dba0a0c93463310ffbd3d9226a56..05128e64e00d2957c99b194124556d0ecb747fff 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -1,19 +1,16 @@ package config import ( - "cmp" "encoding/json" "fmt" "io" "log/slog" "maps" "os" - "os/user" "path/filepath" "runtime" "slices" "strings" - "sync" "github.com/charmbracelet/catwalk/pkg/catwalk" "github.com/charmbracelet/crush/internal/csync" @@ -604,15 +601,3 @@ func GlobalConfigData() string { return filepath.Join(os.Getenv("HOME"), ".local", "share", appName, fmt.Sprintf("%s.json", appName)) } - -var HomeDir = sync.OnceValue(func() string { - u, err := user.Current() - if err == nil { - return u.HomeDir - } - return cmp.Or( - os.Getenv("HOME"), - os.Getenv("USERPROFILE"), - os.Getenv("HOMEPATH"), - ) -}) diff --git a/internal/fsext/home.go b/internal/fsext/home.go new file mode 100644 index 0000000000000000000000000000000000000000..d81a4bc251c0205032a606e91bc53eac9bd43918 --- /dev/null +++ b/internal/fsext/home.go @@ -0,0 +1,20 @@ +package fsext + +import ( + "cmp" + "os" + "os/user" + "sync" +) + +var HomeDir = sync.OnceValue(func() string { + u, err := user.Current() + if err == nil { + return u.HomeDir + } + return cmp.Or( + os.Getenv("HOME"), + os.Getenv("USERPROFILE"), + os.Getenv("HOMEPATH"), + ) +}) diff --git a/internal/fsext/ls.go b/internal/fsext/ls.go index b18e1ef6a5257b03d33ba5a1fca009d7925ff41e..e4b98bb2810d7e5014b881f3b0cac51f4e71965c 100644 --- a/internal/fsext/ls.go +++ b/internal/fsext/ls.go @@ -8,7 +8,6 @@ import ( "sync" "github.com/charlievieth/fastwalk" - "github.com/charmbracelet/crush/internal/config" "github.com/charmbracelet/crush/internal/csync" ignore "github.com/sabhiram/go-gitignore" ) @@ -74,7 +73,7 @@ var commonIgnorePatterns = sync.OnceValue(func() ignore.IgnoreParser { }) var homeIgnore = sync.OnceValue(func() ignore.IgnoreParser { - home := config.HomeDir() + home := HomeDir() var lines []string for _, name := range []string{ filepath.Join(home, ".gitignore"), diff --git a/internal/tui/components/dialogs/models/apikey.go b/internal/tui/components/dialogs/models/apikey.go index 10378ead072f01ed064fe1c48c97abd3c6feb175..80f812cd9c5e92313089aec70f9b9dba4b75375d 100644 --- a/internal/tui/components/dialogs/models/apikey.go +++ b/internal/tui/components/dialogs/models/apikey.go @@ -8,6 +8,7 @@ import ( "github.com/charmbracelet/bubbles/v2/textinput" tea "github.com/charmbracelet/bubbletea/v2" "github.com/charmbracelet/crush/internal/config" + "github.com/charmbracelet/crush/internal/fsext" "github.com/charmbracelet/crush/internal/tui/styles" "github.com/charmbracelet/lipgloss/v2" ) @@ -144,7 +145,7 @@ func (a *APIKeyInput) View() string { inputView := a.input.View() dataPath := config.GlobalConfigData() - dataPath = strings.Replace(dataPath, config.HomeDir(), "~", 1) + dataPath = strings.Replace(dataPath, fsext.HomeDir(), "~", 1) helpText := styles.CurrentTheme().S().Muted. Render(fmt.Sprintf("This will be written to the global configuration: %s", dataPath))