Skip to content

Commit

Permalink
Keep support for -m flag until version 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
boedy committed Sep 16, 2018
1 parent b3cbcec commit 08ee464
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 <[email protected]>"
toAddress: "Donald Duck <[email protected]>>"
fromAddress: "Donald Duck <[email protected]>"
toAddress: "Donald Duck <[email protected]>"
serverHostname: smtp.gmail.com
serverPort: 465
authUsername: [email protected]
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions configGlobal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions configGlobal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 11 additions & 5 deletions duplicacy-util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
cmdPrune bool

sendMail bool
testMailFlag bool
testNotificationsFlag bool

debugFlag bool
Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit 08ee464

Please sign in to comment.