-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(init): clean cache files > 1 week automatically
- Loading branch information
1 parent
8e13ce3
commit 7edd1f0
Showing
5 changed files
with
85 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,7 @@ func runInit(shellName string) { | |
Config: configFlag, | ||
Strict: strict, | ||
Debug: debug, | ||
Init: true, | ||
}, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,7 @@ type Flags struct { | |
Column int | ||
JobCount int | ||
SaveCache bool | ||
Init bool | ||
} | ||
|
||
type CommandError struct { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters