feat: implement XDG Base Directory Specification with github.com/adrg/xdg (#29)

Implement XDG Base Directory Specification using the github.com/adrg/xdg package for robust cross-platform support.
This commit is contained in:
Vito Castellano 2025-10-13 00:39:16 +02:00 committed by GitHub
commit 511e906396
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 18 deletions

1
go.mod
View file

@ -9,6 +9,7 @@ require (
)
require (
github.com/adrg/xdg v0.5.3 // indirect
github.com/gdamore/encoding v1.0.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect

2
go.sum
View file

@ -1,3 +1,5 @@
github.com/adrg/xdg v0.5.3 h1:xRnxJXne7+oWDatRhR1JLnvuccuIeCoBu2rtuLqQB78=
github.com/adrg/xdg v0.5.3/go.mod h1:nlTsY+NNiCBGCK2tpm09vRqfVzrc2fLmXGpBLF0zlTQ=
github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw=
github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo=
github.com/gdamore/tcell/v2 v2.8.1 h1:KPNxyqclpWpWQlPLx6Xui1pMk8S+7+R37h3g07997NU=

View file

@ -14,6 +14,7 @@ import (
"strings"
"sync"
"github.com/adrg/xdg"
"github.com/rivo/tview"
)
@ -22,6 +23,11 @@ const CaskAPIURL = "https://formulae.brew.sh/api/cask.json"
const AnalyticsAPIURL = "https://formulae.brew.sh/api/analytics/install-on-request/90d.json"
const CaskAnalyticsAPIURL = "https://formulae.brew.sh/api/analytics/cask-install/90d.json"
// getCacheDir - returns the cache directory following XDG Base Directory Specification.
func getCacheDir() string {
return filepath.Join(xdg.CacheHome, "bbrew")
}
type BrewServiceInterface interface {
GetPrefixPath() (path string)
GetFormulae() (formulae *[]models.Formula)
@ -303,15 +309,10 @@ func (s *BrewService) loadInstalledCasks() (err error) {
// loadRemote retrieves the list of remote Homebrew formulae from the API and caches them locally.
func (s *BrewService) loadRemote(forceDownload bool) (err error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
bbrewDir := filepath.Join(homeDir, ".bbrew") // TODO: Move to config
formulaFile := filepath.Join(bbrewDir, "formula.json")
if _, err := os.Stat(bbrewDir); os.IsNotExist(err) {
if err := os.MkdirAll(bbrewDir, 0750); err != nil {
cacheDir := getCacheDir()
formulaFile := filepath.Join(cacheDir, "formula.json")
if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
if err := os.MkdirAll(cacheDir, 0750); err != nil {
return err
}
}
@ -354,15 +355,10 @@ func (s *BrewService) loadRemote(forceDownload bool) (err error) {
// loadRemoteCasks retrieves the list of remote Homebrew casks from the API and caches them locally.
func (s *BrewService) loadRemoteCasks(forceDownload bool) (err error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
bbrewDir := filepath.Join(homeDir, ".bbrew")
caskFile := filepath.Join(bbrewDir, "cask.json")
if _, err := os.Stat(bbrewDir); os.IsNotExist(err) {
if err := os.MkdirAll(bbrewDir, 0750); err != nil {
cacheDir := getCacheDir()
caskFile := filepath.Join(cacheDir, "cask.json")
if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
if err := os.MkdirAll(cacheDir, 0750); err != nil {
return err
}
}