From e7e1f0d3a642cf09cfaf32620b80cd92ee2e7c61 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Thu, 2 Dec 2021 14:30:50 +0100 Subject: [PATCH] CLI: Ask to delete cache folder when running reset command #1787 --- internal/commands/reset.go | 89 ++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 22 deletions(-) diff --git a/internal/commands/reset.go b/internal/commands/reset.go index 0cba82d5f..51539301e 100644 --- a/internal/commands/reset.go +++ b/internal/commands/reset.go @@ -16,10 +16,10 @@ import ( "github.com/photoprism/photoprism/internal/entity" ) -// ResetCommand resets the index and removes sidecar files after confirmation. +// ResetCommand resets the index, clears the cache, and removes sidecar backup files after confirmation. var ResetCommand = cli.Command{ Name: "reset", - Usage: "Resets the index and removes generated sidecar files", + Usage: "Resets the index, clears the cache, and removes sidecar backup files", Flags: []cli.Flag{ cli.BoolFlag{ Name: "index, i", @@ -55,7 +55,7 @@ func resetAction(ctx *cli.Context) error { log.Warnf("This will delete and recreate your index database after confirmation") if !ctx.Bool("index") { - log.Warnf("You will be asked next if you also want to remove all JSON and YAML backup files") + log.Warnf("You will be asked next if you also want to remove cache and sidecar files") } } @@ -90,30 +90,45 @@ func resetAction(ctx *cli.Context) error { return nil } + // Clear cache. + removeCachePrompt := promptui.Prompt{ + Label: "Clear cache incl thumbnails?", + IsConfirm: true, + } + + if _, err := removeCachePrompt.Run(); err == nil { + resetCache(conf) + } else { + log.Infof("keeping cache files") + } + + // *.json sidecar files. removeSidecarJsonPrompt := promptui.Prompt{ - Label: "Delete all JSON metadata sidecar files?", + Label: "Delete all *.json sidecar files?", IsConfirm: true, } if _, err := removeSidecarJsonPrompt.Run(); err == nil { resetSidecarJson(conf) } else { - log.Infof("keeping JSON metadata sidecar files") + log.Infof("keeping *.json sidecar files") } + // *.yml backup files. removeSidecarYamlPrompt := promptui.Prompt{ - Label: "Delete all YAML metadata backup files?", + Label: "Delete all *.yml metadata backups?", IsConfirm: true, } if _, err := removeSidecarYamlPrompt.Run(); err == nil { resetSidecarYaml(conf) } else { - log.Infof("keeping YAML metadata backups") + log.Infof("keeping *.yml metadata backups") } + // *.yml album backups. removeAlbumYamlPrompt := promptui.Prompt{ - Label: "Delete all YAML album backup files?", + Label: "Delete all *.yml album backups?", IsConfirm: true, } @@ -127,7 +142,7 @@ func resetAction(ctx *cli.Context) error { } if len(matches) > 0 { - log.Infof("%d YAML album backups will be removed", len(matches)) + log.Infof("%d *.yml album backups will be removed", len(matches)) for _, name := range matches { if err := os.Remove(name); err != nil { @@ -139,12 +154,12 @@ func resetAction(ctx *cli.Context) error { fmt.Println("") - log.Infof("removed all YAML album backups [%s]", time.Since(start)) + log.Infof("removed all *.yml album backups [%s]", time.Since(start)) } else { - log.Infof("found no YAML album backup files") + log.Infof("found no *.yml album backup files") } } else { - log.Infof("keeping YAML album backup files") + log.Infof("keeping *.yml album backup files") } return nil @@ -170,19 +185,49 @@ func resetIndexDb(conf *config.Config) { log.Infof("database reset completed in %s", time.Since(start)) } -// resetSidecarJson removes generated JSON sidecar files. +// resetCache removes all cache files and folders. +func resetCache(conf *config.Config) { + start := time.Now() + + matches, err := filepath.Glob(regexp.QuoteMeta(conf.CachePath()) + "/**") + + if err != nil { + log.Errorf("reset: %s (find cache files)", err) + return + } + + if len(matches) > 0 { + log.Infof("clearing cache") + + for _, name := range matches { + if err := os.RemoveAll(name); err != nil { + fmt.Print("E") + } else { + fmt.Print(".") + } + } + + fmt.Println("") + + log.Infof("removed cache files [%s]", time.Since(start)) + } else { + log.Infof("found no cache files") + } +} + +// resetSidecarJson removes generated *.json sidecar files. func resetSidecarJson(conf *config.Config) { start := time.Now() matches, err := filepath.Glob(regexp.QuoteMeta(conf.SidecarPath()) + "/**/*.json") if err != nil { - log.Errorf("reset: %s (find json sidecar files)", err) + log.Errorf("reset: %s (find *.json sidecar files)", err) return } if len(matches) > 0 { - log.Infof("removing %d JSON metadata sidecar files", len(matches)) + log.Infof("removing %d *.json sidecar files", len(matches)) for _, name := range matches { if err := os.Remove(name); err != nil { @@ -194,25 +239,25 @@ func resetSidecarJson(conf *config.Config) { fmt.Println("") - log.Infof("removed JSON metadata sidecar files [%s]", time.Since(start)) + log.Infof("removed *.json sidecar files [%s]", time.Since(start)) } else { - log.Infof("found no JSON metadata sidecar files") + log.Infof("found no *.json sidecar files") } } -// resetSidecarYaml removes generated YAML sidecar files. +// resetSidecarYaml removes generated *.yml files. func resetSidecarYaml(conf *config.Config) { start := time.Now() matches, err := filepath.Glob(regexp.QuoteMeta(conf.SidecarPath()) + "/**/*.yml") if err != nil { - log.Errorf("reset: %s (find yaml sidecar files)", err) + log.Errorf("reset: %s (find *.yml metadata backups)", err) return } if len(matches) > 0 { - log.Infof("%d YAML metadata backups will be removed", len(matches)) + log.Infof("%d *.yml metadata backups will be removed", len(matches)) for _, name := range matches { if err := os.Remove(name); err != nil { @@ -224,8 +269,8 @@ func resetSidecarYaml(conf *config.Config) { fmt.Println("") - log.Infof("removed all YAML metadata backups [%s]", time.Since(start)) + log.Infof("removed all *.yml metadata backups [%s]", time.Since(start)) } else { - log.Infof("found no YAML metadata backups") + log.Infof("found no *.yml metadata backups") } }