From 293161973dfda78b1a76e9375a668629eadcb958 Mon Sep 17 00:00:00 2001 From: mahanth <22050509+gnmahanth@users.noreply.github.com> Date: Wed, 15 May 2024 13:27:36 +0530 Subject: [PATCH] check and cleanup cache for old files (#75) --- main.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index f2acac0..69b64f3 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "flag" "os" "path" + "path/filepath" "runtime" "strconv" "strings" @@ -98,7 +99,20 @@ func main() { log.Fatal(err) } - tmpPath, tmpErr := os.MkdirTemp(cacheDir, "package-scanner-*") + tmpPathPattern := "package-scanner-*" + + // clean up old cache dir files if present + if dirs, err := filepath.Glob(path.Join(cacheDir, tmpPathPattern)); err != nil { + log.Error(err) + } else { + for _, dir := range dirs { + if err := os.RemoveAll(dir); err != nil { + log.Error(err) + } + } + } + + tmpPath, tmpErr := os.MkdirTemp(cacheDir, tmpPathPattern) if tmpErr != nil { log.Fatal(tmpErr) }