summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-11-21 13:57:07 +0800
committerEltrac <hi@guhub.cn>2025-11-21 13:57:07 +0800
commitc9dd811d81be2433bf5b1de540da639c95a0c7f3 (patch)
tree3787e683ea31ed667196f4fc4fb81ab72423e27f
parent5205a76de15203655f9807578c8b5586f98a82bc (diff)
feat: add cache related flags
- `--no-cache` for lookup without caching - `--clear-cache` to delete all cache files and not look up
-rw-r--r--cache.go5
-rw-r--r--main.go34
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()