adds ignoreDir flag to ignore spcific directories

This commit is contained in:
Philipp Häfelfinger 2020-06-02 21:48:39 +02:00
parent 3a176b33a6
commit c1ad9c9a22
4 changed files with 50 additions and 3 deletions

View File

@ -9,7 +9,6 @@ import (
"flag" "flag"
"git.haefelfinger.net/philipp.haefelfinger/SideCarJpegCleaner/internal/app" "git.haefelfinger.net/philipp.haefelfinger/SideCarJpegCleaner/internal/app"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/vharitonsky/iniflags"
"os" "os"
) )
@ -18,7 +17,7 @@ var (
) )
func main() { func main() {
iniflags.Parse() app.InitializeFlags()
initializeLog() initializeLog()
logrus.Infoln("Starting jpeg cleaner...") logrus.Infoln("Starting jpeg cleaner...")
app.Run() app.Run()

View File

@ -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 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. 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) logLevel = info # The minimum log level required to write out a log message. (panic,fatal,error,warn,info,debug,trace)
ignoreDir = "jpg"
ignoreDir = "png"

View File

@ -9,6 +9,7 @@ import (
"errors" "errors"
"flag" "flag"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/vharitonsky/iniflags"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -18,8 +19,14 @@ import (
var ( var (
imagesRootPath = flag.String("imagesRootPath", "", "This is the images root path that should be cleaned.") 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") 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() { func Run() {
imagesRootFolder := *imagesRootPath imagesRootFolder := *imagesRootPath
if imagesRootFolder == "" { if imagesRootFolder == "" {
@ -47,7 +54,6 @@ func Run() {
} else { } else {
logrus.Warnln("Skipping delete of images as flag dryRun is set to true!") logrus.Warnln("Skipping delete of images as flag dryRun is set to true!")
} }
} }
func deleteFiles(filesWithMissingSidecar []string) { func deleteFiles(filesWithMissingSidecar []string) {
@ -89,12 +95,26 @@ func getJpegsWithoutRawSideCar(jpgFiles []string) []string {
func findAllJpgFiles(basePath string) (error, []string) { func findAllJpgFiles(basePath string) (error, []string) {
var files []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 { err := filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
if strings.HasPrefix(info.Name(), ".") { if strings.HasPrefix(info.Name(), ".") {
logrus.Debugf("Skipping hidden file or directory %s", path) logrus.Debugf("Skipping hidden file or directory %s", path)
if info.IsDir() {
return filepath.SkipDir
}
return nil 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)) extension := strings.ToLower(filepath.Ext(path))
if (extension != ".jpg" && extension != ".jpeg") || info.IsDir() { if (extension != ".jpg" && extension != ".jpeg") || info.IsDir() {
return nil return nil

View File

@ -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
}