From c9dd811d81be2433bf5b1de540da639c95a0c7f3 Mon Sep 17 00:00:00 2001 From: Eltrac Date: Fri, 21 Nov 2025 13:57:07 +0800 Subject: feat: add cache related flags - `--no-cache` for lookup without caching - `--clear-cache` to delete all cache files and not look up --- cache.go | 5 +++++ main.go | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/cache.go b/cache.go index d0b11e5..a81ea66 100644 --- a/cache.go +++ b/cache.go @@ -35,3 +35,8 @@ func WriteCache(relPath string, data []byte) error { path := CachePath(relPath) return os.WriteFile(path, data, 0o644) } + +func ClearCache() error { + path := CachePath("") + return os.RemoveAll(path) +} diff --git a/main.go b/main.go index ae02d5f..03fa05e 100644 --- a/main.go +++ b/main.go @@ -12,11 +12,35 @@ import ( func main() { EnsureBrewAvailable() + var noCache, clearCache bool + // declare CLI app cmd := &cli.Command{ Name: "wthis", Usage: "Fetch information of a Homebrew formulae or cask, like how it got installed, reverse dependencies, etc.", + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "no-cache", + Aliases: []string{"nc"}, + Usage: "Disable caching", + Value: false, + Destination: &noCache, + }, + &cli.BoolFlag{ + Name: "clear-cache", + Aliases: []string{"cc"}, + Usage: "Clear cache", + Value: false, + Destination: &clearCache, + }, + }, Action: func(ctx context.Context, cmd *cli.Command) error { + if clearCache { + ClearCache() + fmt.Println("Cache cleared.") + os.Exit(0) + } + // check args if cmd.Args().Len() == 0 { printErrorAndExit("Please provide a formulae/cask name.") @@ -25,9 +49,8 @@ func main() { // prompt showLoadingPrompt(fmt.Sprintf("What the heck is \"%s\"", pkgName)) // try reading from cache - var stat *statistics - if stat = NewStatisticsFromCache(pkgName); stat == nil { - fmt.Println("cache not hit") + var stat *statistics = NewStatisticsFromCache(pkgName) + if noCache || stat == nil { // cache not hit, then search fmlChan := make(chan *FormulaInfo) caskChan := make(chan *CaskInfo) @@ -42,7 +65,10 @@ func main() { rvsChan <- GetBrewUses(pkgName) }() stat = NewStatistics(<-fmlChan, <-caskChan, pkgName,<-rvsChan) - stat.Cache() + // handle caching + if !noCache { + stat.Cache() + } } // print hideLoadingPrompt() -- cgit v1.2.3