diff options
| -rw-r--r-- | brewcheck.go | 67 | ||||
| -rw-r--r-- | go.mod | 3 | ||||
| -rw-r--r-- | go.sum | 6 |
3 files changed, 20 insertions, 56 deletions
diff --git a/brewcheck.go b/brewcheck.go index b811fa6..8223aff 100644 --- a/brewcheck.go +++ b/brewcheck.go @@ -1,76 +1,37 @@ package main import ( - "encoding/json" - "errors" "os" "os/exec" "path/filepath" "time" "fmt" + "github.com/adrg/xdg" "github.com/fatih/color" ) -type BrewCache struct { - Found bool `json:"found"` - Timestamp time.Time `json:"timestamp"` -} - -// Don't check Homebrew installation for a week const cacheTTL = 7 * 24 * time.Hour func EnsureBrewAvailable() { - ok, err := isBrewAvailable() - if err != nil { - color.Red("⚠️ Error checking Homebrew: %v", err) - os.Exit(1) + cacheFile := filepath.Join(xdg.CacheHome, "wthis", "brew-checked") + + // 1. check if cache file exists and is fresh + if info, err := os.Stat(cacheFile); err == nil { + if time.Since(info.ModTime()) < cacheTTL { + return // recently verified that brew exists + } } - if !ok { + // 2. otherwise, actually check + if _, err := exec.LookPath("brew"); err != nil { + // brew not found — no cache write color.Red("🍺 Homebrew not installed.") fmt.Println("This tool works only with Homebrew formulae and casks.") os.Exit(1) } -} - -func isBrewAvailable() (bool, error) { - cachePath, err := getCacheFile() - if err != nil { - return false, err - } - // Try to read cache - if data, err := os.ReadFile(cachePath); err == nil { - var cache BrewCache - if json.Unmarshal(data, &cache) == nil { - if time.Since(cache.Timestamp) < cacheTTL { - return cache.Found, nil - } - } - } - - // Perform actual check - _, err = exec.LookPath("brew") - found := err == nil - - // Save to cache - _ = os.MkdirAll(filepath.Dir(cachePath), 0o755) - cache := BrewCache{Found: found, Timestamp: time.Now()} - data, _ := json.Marshal(cache) - _ = os.WriteFile(cachePath, data, 0o644) - - return found, nil -} - -func getCacheFile() (string, error) { - base := os.Getenv("XDG_CACHE_HOME") - if base == "" { - home, err := os.UserHomeDir() - if err != nil { - return "", errors.New("cannot determine user home") - } - base = filepath.Join(home, ".cache") - } - return filepath.Join(base, "wthis", "brew-check.json"), nil + // 3. write cache + _ = os.MkdirAll(filepath.Dir(cacheFile), 0o755) + _ = os.WriteFile(cacheFile, []byte{}, 0o644) } @@ -3,6 +3,7 @@ module github.com/BigCoke233/wtfis go 1.25.4 require ( + github.com/adrg/xdg v0.5.3 // direct github.com/fatih/color v1.18.0 // direct github.com/urfave/cli/v3 v3.6.0 // direct ) @@ -10,5 +11,5 @@ require ( require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - golang.org/x/sys v0.25.0 // indirect + golang.org/x/sys v0.26.0 // indirect ) @@ -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/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= @@ -15,7 +17,7 @@ github.com/urfave/cli/v3 v3.6.0 h1:oIdArVjkdIXHWg3iqxgmqwQGC8NM0JtdgwQAj2sRwFo= github.com/urfave/cli/v3 v3.6.0/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= -golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
