Skip to content

Commit 613f323

Browse files
authored
Merge pull request #266 from FluffyEscargot/fix-mutually-exclusive-flags
Make once, cron and begin/frequency flags mutually exclusive
2 parents 738a5d6 + 3c8ad9d commit 613f323

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

cmd/dump.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,11 @@ S3: If it is a URL of the format s3://bucketname/path then it will connect via S
227227
// max-allowed-packet size
228228
flags.Int("max-allowed-packet", defaultMaxAllowedPacket, "Maximum size of the buffer for client/server communication, similar to mysqldump's max_allowed_packet. 0 means to use the default size.")
229229

230+
cmd.MarkFlagsMutuallyExclusive("once", "cron")
231+
cmd.MarkFlagsMutuallyExclusive("once", "begin")
232+
cmd.MarkFlagsMutuallyExclusive("once", "frequency")
233+
cmd.MarkFlagsMutuallyExclusive("cron", "begin")
234+
cmd.MarkFlagsMutuallyExclusive("cron", "frequency")
235+
230236
return cmd, nil
231237
}

cmd/dump_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,41 @@ func TestDumpCmd(t *testing.T) {
3434
Compressor: &compression.GzipCompressor{},
3535
DBConn: database.Connection{Host: "abc"},
3636
}, core.TimerOptions{Frequency: defaultFrequency, Begin: defaultBegin}},
37+
{"once flag", []string{"--server", "abc", "--target", "file:///foo/bar", "--once"}, "", false, core.DumpOptions{
38+
Targets: []storage.Storage{file.New(*fileTargetURL)},
39+
MaxAllowedPacket: defaultMaxAllowedPacket,
40+
Compressor: &compression.GzipCompressor{},
41+
DBConn: database.Connection{Host: "abc"},
42+
}, core.TimerOptions{Frequency: defaultFrequency, Begin: defaultBegin, Once: true}},
43+
{"cron flag", []string{"--server", "abc", "--target", "file:///foo/bar", "--cron", "0 0 * * *"}, "", false, core.DumpOptions{
44+
Targets: []storage.Storage{file.New(*fileTargetURL)},
45+
MaxAllowedPacket: defaultMaxAllowedPacket,
46+
Compressor: &compression.GzipCompressor{},
47+
DBConn: database.Connection{Host: "abc"},
48+
}, core.TimerOptions{Frequency: defaultFrequency, Begin: defaultBegin, Cron: "0 0 * * *"}},
49+
{"begin flag", []string{"--server", "abc", "--target", "file:///foo/bar", "--begin", "1234"}, "", false, core.DumpOptions{
50+
Targets: []storage.Storage{file.New(*fileTargetURL)},
51+
MaxAllowedPacket: defaultMaxAllowedPacket,
52+
Compressor: &compression.GzipCompressor{},
53+
DBConn: database.Connection{Host: "abc"},
54+
}, core.TimerOptions{Frequency: defaultFrequency, Begin: "1234"}},
55+
{"frequency flag", []string{"--server", "abc", "--target", "file:///foo/bar", "--frequency", "10"}, "", false, core.DumpOptions{
56+
Targets: []storage.Storage{file.New(*fileTargetURL)},
57+
MaxAllowedPacket: defaultMaxAllowedPacket,
58+
Compressor: &compression.GzipCompressor{},
59+
DBConn: database.Connection{Host: "abc"},
60+
}, core.TimerOptions{Frequency: 10, Begin: defaultBegin}},
3761
{"config file", []string{"--config-file", "testdata/config.yml"}, "", false, core.DumpOptions{
3862
Targets: []storage.Storage{file.New(*fileTargetURL)},
3963
MaxAllowedPacket: defaultMaxAllowedPacket,
4064
Compressor: &compression.GzipCompressor{},
4165
DBConn: database.Connection{Host: "abc", Port: 3306, User: "user", Pass: "xxxx"},
4266
}, core.TimerOptions{Frequency: defaultFrequency, Begin: defaultBegin}},
67+
{"incompatible flags: once/cron", []string{"--server", "abc", "--target", "file:///foo/bar", "--once", "--cron", "0 0 * * *"}, "", true, core.DumpOptions{}, core.TimerOptions{}},
68+
{"incompatible flags: once/begin", []string{"--server", "abc", "--target", "file:///foo/bar", "--once", "--begin", "1234"}, "", true, core.DumpOptions{}, core.TimerOptions{}},
69+
{"incompatible flags: once/frequency", []string{"--server", "abc", "--target", "file:///foo/bar", "--once", "--frequency", "10"}, "", true, core.DumpOptions{}, core.TimerOptions{}},
70+
{"incompatible flags: cron/begin", []string{"--server", "abc", "--target", "file:///foo/bar", "--cron", "0 0 * * *", "--begin", "1234"}, "", true, core.DumpOptions{}, core.TimerOptions{}},
71+
{"incompatible flags: cron/frequency", []string{"--server", "abc", "--target", "file:///foo/bar", "--cron", "0 0 * * *", "--frequency", "10"}, "", true, core.DumpOptions{}, core.TimerOptions{}},
4372
}
4473

4574
for _, tt := range tests {

pkg/core/timer.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package core
22

33
import (
4-
"errors"
54
"fmt"
65
"regexp"
76
"strconv"
@@ -40,25 +39,14 @@ func Timer(opts TimerOptions) (<-chan Update, error) {
4039
)
4140

4241
now := time.Now().UTC()
43-
44-
// validate we do not have conflicting options
45-
if opts.Once && (opts.Cron != "" || opts.Begin != "" || opts.Frequency != 0) {
46-
return nil, errors.New("option 'Once' is exclusive and must not be used with Begin, Cron or Frequency")
47-
}
48-
49-
if opts.Cron != "" && (opts.Begin != "" || opts.Frequency != 0) {
50-
return nil, errors.New("option 'Cron' is exclusive and must not be used with Begin, Once or Frequency")
51-
}
52-
5342
// parse the options to determine our delays
5443
if opts.Cron != "" {
5544
// calculate delay until next cron moment as defined
5645
delay, err = waitForCron(opts.Cron, now)
5746
if err != nil {
5847
return nil, fmt.Errorf("invalid cron format '%s': %v", opts.Cron, err)
5948
}
60-
}
61-
if opts.Begin != "" {
49+
} else if opts.Begin != "" {
6250
// calculate how long to wait
6351
minsRe, err := regexp.Compile(`^\+([0-9]+)$`)
6452
if err != nil {

0 commit comments

Comments
 (0)