feat(init): clean cache files > 1 week automatically
Some checks are pending
Code QL / code-ql (push) Waiting to run
Azure Static Web Apps CI/CD / Build and Deploy (push) Waiting to run
Release / changelog (push) Waiting to run
Release / artifacts (push) Blocked by required conditions

This commit is contained in:
Jan De Dobbeleer 2024-10-13 12:48:11 +02:00 committed by Jan De Dobbeleer
parent b239a68d05
commit 49ea48934f
5 changed files with 85 additions and 25 deletions

57
src/cache/clear.go vendored Normal file
View file

@ -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
}

View file

@ -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)
}
}
}

View file

@ -82,6 +82,7 @@ func runInit(shellName string) {
Config: configFlag,
Strict: strict,
Debug: debug,
Init: true,
},
}

View file

@ -100,6 +100,7 @@ type Flags struct {
Column int
JobCount int
SaveCache bool
Init bool
}
type CommandError struct {

View file

@ -596,10 +596,27 @@ func (term *Terminal) saveTemplateCache() {
func (term *Terminal) Close() {
defer term.Trace(time.Now())
term.saveTemplateCache()
term.clearCacheFiles()
term.deviceCache.Close()
term.sessionCache.Close()
}
func (term *Terminal) clearCacheFiles() {
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())