From c1ad9c9a22f5ce26e32864fc281b21467640985c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Tue, 2 Jun 2020 21:48:39 +0200 Subject: [PATCH] adds ignoreDir flag to ignore spcific directories --- cmd/SideCarJpegCleaner/SideCarJpegCleaner.go | 3 +-- configs/defaultConfig.ini | 2 ++ internal/app/app.go | 22 ++++++++++++++++- internal/app/arrayFlags.go | 26 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 internal/app/arrayFlags.go diff --git a/cmd/SideCarJpegCleaner/SideCarJpegCleaner.go b/cmd/SideCarJpegCleaner/SideCarJpegCleaner.go index 457a377..90a3642 100644 --- a/cmd/SideCarJpegCleaner/SideCarJpegCleaner.go +++ b/cmd/SideCarJpegCleaner/SideCarJpegCleaner.go @@ -9,7 +9,6 @@ import ( "flag" "git.haefelfinger.net/philipp.haefelfinger/SideCarJpegCleaner/internal/app" "github.com/sirupsen/logrus" - "github.com/vharitonsky/iniflags" "os" ) @@ -18,7 +17,7 @@ var ( ) func main() { - iniflags.Parse() + app.InitializeFlags() initializeLog() logrus.Infoln("Starting jpeg cleaner...") app.Run() diff --git a/configs/defaultConfig.ini b/configs/defaultConfig.ini index 06fbe41..34d877e 100644 --- a/configs/defaultConfig.ini +++ b/configs/defaultConfig.ini @@ -4,3 +4,5 @@ configUpdateInterval = 0s # Update interval for re-reading config file set via dryRun = true # If set to true, all actions are run except the real filesystem modifications imagesRootPath = # This is the images root path that should be mirrored to piwigo. logLevel = info # The minimum log level required to write out a log message. (panic,fatal,error,warn,info,debug,trace) +ignoreDir = "jpg" +ignoreDir = "png" \ No newline at end of file diff --git a/internal/app/app.go b/internal/app/app.go index 7e494ac..0b722b4 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -9,6 +9,7 @@ import ( "errors" "flag" "github.com/sirupsen/logrus" + "github.com/vharitonsky/iniflags" "os" "path" "path/filepath" @@ -18,8 +19,14 @@ import ( var ( imagesRootPath = flag.String("imagesRootPath", "", "This is the images root path that should be cleaned.") dryRun = flag.Bool("dryRun", true, "If set to true, all actions are run except the real filesystem modifications") + ignoreDirs arrayFlags ) +func InitializeFlags() { + flag.Var(&ignoreDirs, "ignoreDir", "Directories that should be ignored. Flag can be specified multiple times for more than one directory.") + iniflags.Parse() +} + func Run() { imagesRootFolder := *imagesRootPath if imagesRootFolder == "" { @@ -47,7 +54,6 @@ func Run() { } else { logrus.Warnln("Skipping delete of images as flag dryRun is set to true!") } - } func deleteFiles(filesWithMissingSidecar []string) { @@ -89,12 +95,26 @@ func getJpegsWithoutRawSideCar(jpgFiles []string) []string { func findAllJpgFiles(basePath string) (error, []string) { var files []string + ignoreDirsMap := make(map[string]struct{}, len(ignoreDirs)) + for _, ignoredFolder := range ignoreDirs { + ignoreDirsMap[strings.ToLower(ignoredFolder)] = struct{}{} + } + err := filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error { if strings.HasPrefix(info.Name(), ".") { logrus.Debugf("Skipping hidden file or directory %s", path) + if info.IsDir() { + return filepath.SkipDir + } return nil } + _, dirIgnored := ignoreDirsMap[strings.ToLower(info.Name())] + if dirIgnored && info.IsDir() { + logrus.Tracef("Skipping ignored directory %s", path) + return filepath.SkipDir + } + extension := strings.ToLower(filepath.Ext(path)) if (extension != ".jpg" && extension != ".jpeg") || info.IsDir() { return nil diff --git a/internal/app/arrayFlags.go b/internal/app/arrayFlags.go new file mode 100644 index 0000000..1bccfb2 --- /dev/null +++ b/internal/app/arrayFlags.go @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2019 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved. + * This application is licensed under GPLv2. See the LICENSE file in the root directory of the project. + */ + +package app + +import "strings" + +type arrayFlags []string + +func (arr *arrayFlags) String() string { + b := strings.Builder{} + for _, v := range *arr { + if b.Len() > 0 { + b.WriteString(",") + } + b.WriteString(v) + } + return b.String() +} + +func (arr *arrayFlags) Set(value string) error { + *arr = append(*arr, strings.TrimSpace(value)) + return nil +}