mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-24 18:44:04 -08:00
feat(init): clean cache files > 1 week automatically
This commit is contained in:
parent
b239a68d05
commit
49ea48934f
57
src/cache/clear.go
vendored
Normal file
57
src/cache/clear.go
vendored
Normal 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
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,6 +82,7 @@ func runInit(shellName string) {
|
|||
Config: configFlag,
|
||||
Strict: strict,
|
||||
Debug: debug,
|
||||
Init: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -100,6 +100,7 @@ type Flags struct {
|
|||
Column int
|
||||
JobCount int
|
||||
SaveCache bool
|
||||
Init bool
|
||||
}
|
||||
|
||||
type CommandError struct {
|
||||
|
|
|
@ -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())
|
||||
|
||||
|
|
Loading…
Reference in a new issue