From d95dcecda2cd9b6144148c182df1b1fabc2b8ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Tue, 14 Apr 2020 23:14:11 +0200 Subject: [PATCH] moves all flags from the entry point to flags.go --- .../PiwigoDirectoryUploader.go | 22 -------- internal/app/app.go | 34 ++++++------- internal/app/flags.go | 51 +++++++++++++++++++ 3 files changed, 67 insertions(+), 40 deletions(-) create mode 100644 internal/app/flags.go diff --git a/cmd/PiwigoDirectoryUploader/PiwigoDirectoryUploader.go b/cmd/PiwigoDirectoryUploader/PiwigoDirectoryUploader.go index 306f52c..0b39df6 100644 --- a/cmd/PiwigoDirectoryUploader/PiwigoDirectoryUploader.go +++ b/cmd/PiwigoDirectoryUploader/PiwigoDirectoryUploader.go @@ -6,31 +6,9 @@ package main import ( - "flag" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/app" - "github.com/sirupsen/logrus" - "github.com/vharitonsky/iniflags" - "os" -) - -var ( - logLevel = flag.String("logLevel", "info", "The minimum log level required to write out a log message. (panic,fatal,error,warn,info,debug,trace)") ) func main() { - iniflags.Parse() - initializeLog() app.Run() } - -func initializeLog() { - level, err := logrus.ParseLevel(*logLevel) - if err != nil { - level = logrus.DebugLevel - } - logrus.SetLevel(level) - - logrus.SetOutput(os.Stdout) - - logrus.Infoln("Starting Piwigo directories to albums...") -} diff --git a/internal/app/app.go b/internal/app/app.go index 2a78122..79f1c95 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -6,7 +6,6 @@ package app import ( - "flag" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/category" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/images" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure" @@ -14,18 +13,10 @@ import ( "os" ) -var ( - imagesRootPath = flag.String("imagesRootPath", "", "This is the images root path that should be mirrored to piwigo.") - sqliteDb = flag.String("sqliteDb", "./localstate.db", "The connection string to the sql lite database file.") - noUpload = flag.Bool("noUpload", false, "If set to true, the metadata gets prepared but the upload is not called and the application is exited with code 90") - piwigoUrl = flag.String("piwigoUrl", "", "The root url without tailing slash to your piwigo installation.") - piwigoUser = flag.String("piwigoUser", "", "The username to use during sync.") - piwigoPassword = flag.String("piwigoPassword", "", "This is password to the given username.") - removeImages = flag.Bool("removeImages", false, "If set to true, images scheduled to delete will be removed from the piwigo server. Be sure you want to delete images before enabling this flag.") - parallelUploads = flag.Int("parallelUploads", 4, "Set the number of images that get uploaded in parallel.") -) - func Run() { + initializeFlags() + initializeLog() + context, err := newAppContext() if err != nil { logErrorAndExit(err, 1) @@ -36,12 +27,7 @@ func Run() { logErrorAndExit(err, 2) } - //TODO: make params here as flags - supportedExtensions := make([]string, 0) - supportedExtensions = append(supportedExtensions, "jpg") - supportedExtensions = append(supportedExtensions, "png") - - filesystemNodes, err := localFileStructure.ScanLocalFileStructure(context.localRootPath, supportedExtensions, make([]string,0)) + filesystemNodes, err := localFileStructure.ScanLocalFileStructure(context.localRootPath, extensions, ignoreDirs, *dirSuffixToSkip) if err != nil { logErrorAndExit(err, 3) } @@ -82,6 +68,18 @@ func Run() { _ = context.piwigo.Logout() } +func initializeLog() { + level, err := logrus.ParseLevel(*logLevel) + if err != nil { + level = logrus.DebugLevel + } + logrus.SetLevel(level) + + logrus.SetOutput(os.Stdout) + + logrus.Infoln("Starting Piwigo directories to albums...") +} + func logErrorAndExit(err error, exitCode int) { logrus.Errorln(err) os.Exit(exitCode) diff --git a/internal/app/flags.go b/internal/app/flags.go new file mode 100644 index 0000000..0264617 --- /dev/null +++ b/internal/app/flags.go @@ -0,0 +1,51 @@ +/* + * 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 ( + "flag" + "github.com/vharitonsky/iniflags" + "strings" +) + +var ( + logLevel = flag.String("logLevel", "info", "The minimum log level required to write out a log message. (panic,fatal,error,warn,info,debug,trace)") + imagesRootPath = flag.String("imagesRootPath", "", "This is the images root path that should be mirrored to piwigo.") + sqliteDb = flag.String("sqliteDb", "./localstate.db", "The connection string to the sql lite database file.") + noUpload = flag.Bool("noUpload", false, "If set to true, the metadata gets prepared but the upload is not called and the application is exited with code 90") + piwigoUrl = flag.String("piwigoUrl", "", "The root url without tailing slash to your piwigo installation.") + piwigoUser = flag.String("piwigoUser", "", "The username to use during sync.") + piwigoPassword = flag.String("piwigoPassword", "", "This is password to the given username.") + removeImages = flag.Bool("removeImages", false, "If set to true, images scheduled to delete will be removed from the piwigo server. Be sure you want to delete images before enabling this flag.") + parallelUploads = flag.Int("parallelUploads", 4, "Set the number of images that get uploaded in parallel.") + dirSuffixToSkip = flag.Int("dirSuffixToSkip", 0, "Set the number of directories at the end of the filepath to remove to build the category (e.g. value of 1: /foo/png/img.png results in foo/img.png).") + extensions arrayFlags + ignoreDirs arrayFlags +) + +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 +} + +func initializeFlags() { + flag.Var(&extensions, "extension", "Supported file extensions. Flag can be specified multiple times. Uses jpg and png if omitted.") + flag.Var(&ignoreDirs, "ignoreDir", "Directories that should be ignored. Flag can be specified multiple times for more than one directory.") + iniflags.Parse() +}