From 08ee464c1d3156a09bdcaaa95b3aa264814b0533 Mon Sep 17 00:00:00 2001 From: boedy Date: Sat, 15 Sep 2018 22:06:51 +0200 Subject: [PATCH] Keep support for -m flag until version 2.0 --- README.md | 22 +++++++++++++--------- configGlobal.go | 10 ++++++---- configGlobal_test.go | 8 ++++++++ duplicacy-util.go | 16 +++++++++++----- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9bdbdd2..e74f8b4 100644 --- a/README.md +++ b/README.md @@ -158,17 +158,16 @@ The following fields are checked in the global configuration file: #### Notifications `Duplicacy-util` supports notifying you when backups succeed, fail and start. Unless you're planning to only be running `dupliacy-util` interactively, it's strongly recommended -to configure notifications. By default `dupliacy-util` notifies you on success and failure -which is equal to the following example configuration: +to configure notifications. +For now only email notifications are supported, but more notification channels +are on it's way. The following config snippet shows how to subscribe to specific +notifications: ``` notifications: onSuccess: ['email'] onFailure: ['email'] onStart: [] ``` -The above config has support for multipe notification channels, but currently only -email notifications are supported. To following section describes how to setup -email notifications. ##### Email notifications @@ -181,12 +180,17 @@ email notifications. | authUsername | Username for authentication with SMTP server | | authPassword | Password for authentication with SMTP server | -Example configuration: +Here is an example how to setup email notifications: ``` +notifications: + onSuccess: ['email'] + onFailure: ['email'] + onStart: [] + email: - fromAddress: "Donald Duck <donald.xyzzy@gmail.com>" - toAddress: "Donald Duck <donald.xyzzy@gmail.com>>" + fromAddress: "Donald Duck " + toAddress: "Donald Duck " serverHostname: smtp.gmail.com serverPort: 465 authUsername: donald.xyzzy@gmail.com @@ -404,7 +408,7 @@ Usage of ./duplicacy-util: Configuration file for storage definitions (must be specified) -g string Global configuration file name - -m Send E-Mail with results of operations (implies quiet) + -m (Deprecated) Send E-Mail with results of operations (implies quiet) -p Perform duplicacy prune operation -q Quiet operations (generate output only in case of error) -sd string diff --git a/configGlobal.go b/configGlobal.go index 805a4b1..102c6c9 100644 --- a/configGlobal.go +++ b/configGlobal.go @@ -105,6 +105,9 @@ func setGlobalConfigVariables(storageDir string, cfgFile string) error { return err case viper.ConfigFileNotFoundError: // No configuration file is okay unless we specifically asked for a named file + if sendMail { + return errors.New("No global config found: Can't use -m or -tm flags") + } if cfgFile != "" { return err } @@ -130,12 +133,11 @@ func setGlobalConfigVariables(storageDir string, cfgFile string) error { globalLogFileCount = configInt } - // Try to set default notifiers if non are defined in the config - if viper.IsSet("notifications") == false { + // Support for deprecated -m flag until it gets removed + if sendMail { defaultNotifier, err := NewEmailNotifier() if err != nil { - // No email configuration found - return nil + return err } onFailureNotifiers = append(onFailureNotifiers, defaultNotifier) onStartNotifiers = append(onStartNotifiers, defaultNotifier) diff --git a/configGlobal_test.go b/configGlobal_test.go index e2f5365..8a435e0 100644 --- a/configGlobal_test.go +++ b/configGlobal_test.go @@ -97,6 +97,14 @@ func TestSendMailFlagWithInvalidEmailConfig(t *testing.T) { } } +func TestSendMailFlagWithNoConfig(t *testing.T) { + sendMail = true + err := loadGlobalConfig(".", "") + if err == nil { + t.Error("Invalid E-main configuration error should have been returned") + } +} + func TestSendMailFlagWithValidEmailConfig(t *testing.T) { sendMail = true err := loadGlobalConfig(".", "test/assets/globalConfigs/fullValidConfig.yml") diff --git a/duplicacy-util.go b/duplicacy-util.go index 9e6349b..e115fd3 100644 --- a/duplicacy-util.go +++ b/duplicacy-util.go @@ -40,6 +40,7 @@ var ( cmdPrune bool sendMail bool + testMailFlag bool testNotificationsFlag bool debugFlag bool @@ -77,8 +78,8 @@ func init() { flag.BoolVar(&cmdCheck, "c", false, "Perform duplicacy check operation") flag.BoolVar(&cmdPrune, "p", false, "Perform duplicacy prune operation") - flag.BoolVar(&sendMail, "m", false, "Send E-Mail with results of operations (implies quiet)") - flag.BoolVar(&testNotificationsFlag, "tm", false, "(Deprecated: Use -tn instead) Send a test message via E-Mail") + flag.BoolVar(&sendMail, "m", false, "(Deprecated) Send E-Mail with results of operations (implies quiet)") + flag.BoolVar(&testMailFlag, "tm", false, "(Deprecated: Use -tn instead) Send a test message via E-Mail") flag.BoolVar(&testNotificationsFlag, "tn", false, "Test notifications") flag.BoolVar(&debugFlag, "d", false, "Enable debug output (implies verbose)") @@ -141,6 +142,11 @@ func main() { os.Exit(0) } + if testMailFlag { + sendMail = true + testNotificationsFlag = true + } + // Determine the location of the global storage directory globalStorageDirectory, err = getStorageDirectory(cmdStorageDir) if err != nil { @@ -156,9 +162,9 @@ func main() { returnStatus, err := processArguments() if err != nil { - // Notify all configure channels that the backup process has faile - notifyOfFailure() + // Notify that the backup process has failed logError(nil, fmt.Sprintf("Error: %s", err)) + notifyOfFailure() } os.Exit(returnStatus) @@ -178,7 +184,7 @@ func processArguments() (int, error) { quietFlag = false } - // if no failure notifier is defined quite mode is not allowed + // if no failure notifier is defined quiet mode is not allowed if quietFlag && hasFailureNotifier() == false { quietFlag = false logError(nil, "Notice: Quiet mode refused; makes no sense without sending mail")