From 7edd1f0babae233a9c06fe7f7b914fb6c4ecd4a4 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 13 Oct 2024 12:48:11 +0200 Subject: [PATCH] feat(init): clean cache files > 1 week automatically --- src/cache/clear.go | 57 ++++++++++++++++++++++++++++++++++++++ src/cli/cache.go | 34 ++++++----------------- src/cli/init.go | 1 + src/runtime/environment.go | 1 + src/runtime/terminal.go | 17 ++++++++++++ 5 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 src/cache/clear.go diff --git a/src/cache/clear.go b/src/cache/clear.go new file mode 100644 index 000000000000..16b06d3509c0 --- /dev/null +++ b/src/cache/clear.go @@ -0,0 +1,57 @@ +package cache + +import ( + "os" + "path/filepath" + "strings" +) + +func Clear(cachePath string, force bool) ([]string, error) { + // get all files in the cache directory that start with omp.cache and delete them + files, err := os.ReadDir(cachePath) + if err != nil { + return []string{}, err + } + + var removed []string + + deleteFile := func(file string) { + path := filepath.Join(cachePath, file) + if err := os.Remove(path); err == nil { + removed = append(removed, path) + } + } + + for _, file := range files { + if file.IsDir() { + continue + } + + if !strings.HasPrefix(file.Name(), FileName) { + continue + } + + if force { + deleteFile(file.Name()) + continue + } + + // don't delete the system cache file unless forced + if file.Name() == FileName { + continue + } + + info, err := file.Info() + if err != nil { + continue + } + + if info.ModTime().AddDate(0, 0, 7).After(info.ModTime()) { + continue + } + + deleteFile(file.Name()) + } + + return removed, nil +} diff --git a/src/cli/cache.go b/src/cli/cache.go index 1ea505438594..91bc206c2bdc 100644 --- a/src/cli/cache.go +++ b/src/cli/cache.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "path/filepath" - "strings" "github.com/jandedobbeleer/oh-my-posh/src/cache" "github.com/jandedobbeleer/oh-my-posh/src/runtime" @@ -46,7 +45,15 @@ You can do the following: case "path": fmt.Println(env.CachePath()) case "clear": - clear(env.CachePath()) + deletedFiles, err := cache.Clear(env.CachePath(), true) + if err != nil { + fmt.Println(err) + return + } + + for _, file := range deletedFiles { + fmt.Println("removed cache file:", file) + } case "edit": cacheFilePath := filepath.Join(env.CachePath(), cache.FileName) os.Exit(editFileWithEditor(cacheFilePath)) @@ -57,26 +64,3 @@ You can do the following: func init() { RootCmd.AddCommand(getCache) } - -func clear(cachePath string) { - // get all files in the cache directory that start with omp.cache and delete them - files, err := os.ReadDir(cachePath) - if err != nil { - return - } - - for _, file := range files { - if file.IsDir() { - continue - } - - if !strings.HasPrefix(file.Name(), cache.FileName) { - continue - } - - path := filepath.Join(cachePath, file.Name()) - if err := os.Remove(path); err == nil { - fmt.Println("removed cache file:", path) - } - } -} diff --git a/src/cli/init.go b/src/cli/init.go index e8754ea83d49..1f4cf395ed3c 100644 --- a/src/cli/init.go +++ b/src/cli/init.go @@ -82,6 +82,7 @@ func runInit(shellName string) { Config: configFlag, Strict: strict, Debug: debug, + Init: true, }, } diff --git a/src/runtime/environment.go b/src/runtime/environment.go index d1e6e79cfee9..98b79fdad902 100644 --- a/src/runtime/environment.go +++ b/src/runtime/environment.go @@ -100,6 +100,7 @@ type Flags struct { Column int JobCount int SaveCache bool + Init bool } type CommandError struct { diff --git a/src/runtime/terminal.go b/src/runtime/terminal.go index c9bf444f78be..33f78c98e7e9 100644 --- a/src/runtime/terminal.go +++ b/src/runtime/terminal.go @@ -596,10 +596,27 @@ func (term *Terminal) saveTemplateCache() { func (term *Terminal) Close() { defer term.Trace(time.Now()) term.saveTemplateCache() + term.cleanCacheFiles() term.deviceCache.Close() term.sessionCache.Close() } +func (term *Terminal) cleanCacheFiles() { + if !term.CmdFlags.Init { + return + } + + deletedFiles, err := cache.Clear(term.CachePath(), false) + if err != nil { + term.Error(err) + return + } + + for _, file := range deletedFiles { + term.DebugF("removed cache file: %s", file) + } +} + func (term *Terminal) LoadTemplateCache() { defer term.Trace(time.Now())