refactor: move `HomeDir()` to `fsext` package

Andrey Nering created

Change summary

internal/config/load.go                          | 15 -------------
internal/fsext/home.go                           | 20 ++++++++++++++++++
internal/fsext/ls.go                             |  3 -
internal/tui/components/dialogs/models/apikey.go |  3 +
4 files changed, 23 insertions(+), 18 deletions(-)

Detailed changes

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"),
-	)
-})

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"),
+	)
+})

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"),

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))