CLI: Ask to delete cache folder when running reset command #1787

This commit is contained in:
Michael Mayer 2021-12-02 14:30:50 +01:00
parent d0a291a679
commit e7e1f0d3a6

View file

@ -16,10 +16,10 @@ import (
"github.com/photoprism/photoprism/internal/entity" "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{ var ResetCommand = cli.Command{
Name: "reset", 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{ Flags: []cli.Flag{
cli.BoolFlag{ cli.BoolFlag{
Name: "index, i", 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") log.Warnf("This will delete and recreate your index database after confirmation")
if !ctx.Bool("index") { 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 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{ removeSidecarJsonPrompt := promptui.Prompt{
Label: "Delete all JSON metadata sidecar files?", Label: "Delete all *.json sidecar files?",
IsConfirm: true, IsConfirm: true,
} }
if _, err := removeSidecarJsonPrompt.Run(); err == nil { if _, err := removeSidecarJsonPrompt.Run(); err == nil {
resetSidecarJson(conf) resetSidecarJson(conf)
} else { } else {
log.Infof("keeping JSON metadata sidecar files") log.Infof("keeping *.json sidecar files")
} }
// *.yml backup files.
removeSidecarYamlPrompt := promptui.Prompt{ removeSidecarYamlPrompt := promptui.Prompt{
Label: "Delete all YAML metadata backup files?", Label: "Delete all *.yml metadata backups?",
IsConfirm: true, IsConfirm: true,
} }
if _, err := removeSidecarYamlPrompt.Run(); err == nil { if _, err := removeSidecarYamlPrompt.Run(); err == nil {
resetSidecarYaml(conf) resetSidecarYaml(conf)
} else { } else {
log.Infof("keeping YAML metadata backups") log.Infof("keeping *.yml metadata backups")
} }
// *.yml album backups.
removeAlbumYamlPrompt := promptui.Prompt{ removeAlbumYamlPrompt := promptui.Prompt{
Label: "Delete all YAML album backup files?", Label: "Delete all *.yml album backups?",
IsConfirm: true, IsConfirm: true,
} }
@ -127,7 +142,7 @@ func resetAction(ctx *cli.Context) error {
} }
if len(matches) > 0 { 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 { for _, name := range matches {
if err := os.Remove(name); err != nil { if err := os.Remove(name); err != nil {
@ -139,12 +154,12 @@ func resetAction(ctx *cli.Context) error {
fmt.Println("") 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 { } else {
log.Infof("found no YAML album backup files") log.Infof("found no *.yml album backup files")
} }
} else { } else {
log.Infof("keeping YAML album backup files") log.Infof("keeping *.yml album backup files")
} }
return nil return nil
@ -170,19 +185,49 @@ func resetIndexDb(conf *config.Config) {
log.Infof("database reset completed in %s", time.Since(start)) 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) { func resetSidecarJson(conf *config.Config) {
start := time.Now() start := time.Now()
matches, err := filepath.Glob(regexp.QuoteMeta(conf.SidecarPath()) + "/**/*.json") matches, err := filepath.Glob(regexp.QuoteMeta(conf.SidecarPath()) + "/**/*.json")
if err != nil { if err != nil {
log.Errorf("reset: %s (find json sidecar files)", err) log.Errorf("reset: %s (find *.json sidecar files)", err)
return return
} }
if len(matches) > 0 { 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 { for _, name := range matches {
if err := os.Remove(name); err != nil { if err := os.Remove(name); err != nil {
@ -194,25 +239,25 @@ func resetSidecarJson(conf *config.Config) {
fmt.Println("") fmt.Println("")
log.Infof("removed JSON metadata sidecar files [%s]", time.Since(start)) log.Infof("removed *.json sidecar files [%s]", time.Since(start))
} else { } 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) { func resetSidecarYaml(conf *config.Config) {
start := time.Now() start := time.Now()
matches, err := filepath.Glob(regexp.QuoteMeta(conf.SidecarPath()) + "/**/*.yml") matches, err := filepath.Glob(regexp.QuoteMeta(conf.SidecarPath()) + "/**/*.yml")
if err != nil { if err != nil {
log.Errorf("reset: %s (find yaml sidecar files)", err) log.Errorf("reset: %s (find *.yml metadata backups)", err)
return return
} }
if len(matches) > 0 { 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 { for _, name := range matches {
if err := os.Remove(name); err != nil { if err := os.Remove(name); err != nil {
@ -224,8 +269,8 @@ func resetSidecarYaml(conf *config.Config) {
fmt.Println("") 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 { } else {
log.Infof("found no YAML metadata backups") log.Infof("found no *.yml metadata backups")
} }
} }