on main help sign in
refactor: move `HomeDir()` to `fsext` package
172df74Andrey Nering
internal/config/load.go−15
@@ -1,19 +1,16 @@
1package config
1package config
2
2
3import (
3import (
4− "cmp"
5 "encoding/json"
4 "encoding/json"
6 "fmt"
5 "fmt"
7 "io"
6 "io"
8 "log/slog"
7 "log/slog"
9 "maps"
8 "maps"
10 "os"
9 "os"
11− "os/user"
12 "path/filepath"
10 "path/filepath"
13 "runtime"
11 "runtime"
14 "slices"
12 "slices"
15 "strings"
13 "strings"
16− "sync"
17
14
18 "github.com/charmbracelet/catwalk/pkg/catwalk"
15 "github.com/charmbracelet/catwalk/pkg/catwalk"
19 "github.com/charmbracelet/crush/internal/csync"
16 "github.com/charmbracelet/crush/internal/csync"
@@ -604,15 +601,3 @@ func GlobalConfigData() string {
604
601
605 return filepath.Join(os.Getenv("HOME"), ".local", "share", appName, fmt.Sprintf("%s.json", appName))
602 return filepath.Join(os.Getenv("HOME"), ".local", "share", appName, fmt.Sprintf("%s.json", appName))
606}
603}
607−
608−var HomeDir = sync.OnceValue(func() string {
609− u, err := user.Current()
610− if err == nil {
611− return u.HomeDir
612− }
613− return cmp.Or(
614− os.Getenv("HOME"),
615− os.Getenv("USERPROFILE"),
616− os.Getenv("HOMEPATH"),
617− )
618−})
internal/fsext/home.gonew file+20
@@ -0,0 +1,20 @@
1+package fsext
2+
3+import (
4+ "cmp"
5+ "os"
6+ "os/user"
7+ "sync"
8+)
9+
10+var HomeDir = sync.OnceValue(func() string {
11+ u, err := user.Current()
12+ if err == nil {
13+ return u.HomeDir
14+ }
15+ return cmp.Or(
16+ os.Getenv("HOME"),
17+ os.Getenv("USERPROFILE"),
18+ os.Getenv("HOMEPATH"),
19+ )
20+})
internal/fsext/ls.go+1−2
@@ -8,7 +8,6 @@ import (
8 "sync"
8 "sync"
9
9
10 "github.com/charlievieth/fastwalk"
10 "github.com/charlievieth/fastwalk"
11− "github.com/charmbracelet/crush/internal/config"
12 "github.com/charmbracelet/crush/internal/csync"
11 "github.com/charmbracelet/crush/internal/csync"
13 ignore "github.com/sabhiram/go-gitignore"
12 ignore "github.com/sabhiram/go-gitignore"
14)
13)
@@ -74,7 +73,7 @@ var commonIgnorePatterns = sync.OnceValue(func() ignore.IgnoreParser {
74})
73})
75
74
76var homeIgnore = sync.OnceValue(func() ignore.IgnoreParser {
75var homeIgnore = sync.OnceValue(func() ignore.IgnoreParser {
77− home := config.HomeDir()
76+ home := HomeDir()
78 var lines []string
77 var lines []string
79 for _, name := range []string{
78 for _, name := range []string{
80 filepath.Join(home, ".gitignore"),
79 filepath.Join(home, ".gitignore"),
internal/tui/components/dialogs/models/apikey.go+2−1
@@ -8,6 +8,7 @@ import (
8 "github.com/charmbracelet/bubbles/v2/textinput"
8 "github.com/charmbracelet/bubbles/v2/textinput"
9 tea "github.com/charmbracelet/bubbletea/v2"
9 tea "github.com/charmbracelet/bubbletea/v2"
10 "github.com/charmbracelet/crush/internal/config"
10 "github.com/charmbracelet/crush/internal/config"
11+ "github.com/charmbracelet/crush/internal/fsext"
11 "github.com/charmbracelet/crush/internal/tui/styles"
12 "github.com/charmbracelet/crush/internal/tui/styles"
12 "github.com/charmbracelet/lipgloss/v2"
13 "github.com/charmbracelet/lipgloss/v2"
13)
14)
@@ -144,7 +145,7 @@ func (a *APIKeyInput) View() string {
144 inputView := a.input.View()
145 inputView := a.input.View()
145
146
146 dataPath := config.GlobalConfigData()
147 dataPath := config.GlobalConfigData()
147− dataPath = strings.Replace(dataPath, config.HomeDir(), "~", 1)
148+ dataPath = strings.Replace(dataPath, fsext.HomeDir(), "~", 1)
148 helpText := styles.CurrentTheme().S().Muted.
149 helpText := styles.CurrentTheme().S().Muted.
149 Render(fmt.Sprintf("This will be written to the global configuration: %s", dataPath))
150 Render(fmt.Sprintf("This will be written to the global configuration: %s", dataPath))
150
151