Skip to content

Commit

Permalink
feat(init): clean cache files > 1 week automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Oct 13, 2024
1 parent 8e13ce3 commit 7edd1f0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 25 deletions.
57 changes: 57 additions & 0 deletions src/cache/clear.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 9 additions & 25 deletions src/cli/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
}
}
1 change: 1 addition & 0 deletions src/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func runInit(shellName string) {
Config: configFlag,
Strict: strict,
Debug: debug,
Init: true,
},
}

Expand Down
1 change: 1 addition & 0 deletions src/runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type Flags struct {
Column int
JobCount int
SaveCache bool
Init bool
}

type CommandError struct {
Expand Down
17 changes: 17 additions & 0 deletions src/runtime/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 7edd1f0

Please sign in to comment.