From 64e75face8fd224ad6ca4b3bbab035dc4351194e Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sun, 5 Feb 2017 15:15:28 -0800 Subject: [PATCH] Add maxage flag to match subcommand --- cmd/client/match.go | 13 ++++++++++++- cmd/client/mbox.go | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cmd/client/match.go b/cmd/client/match.go index f6ab45d..44c0830 100644 --- a/cmd/client/match.go +++ b/cmd/client/match.go @@ -6,6 +6,7 @@ import ( "flag" "fmt" "os" + "time" "github.com/google/subcommands" "github.com/jhillyerd/inbucket/rest/client" @@ -16,9 +17,11 @@ type matchCmd struct { output string outFunc func(headers []*client.MessageHeader) error delete bool + // match criteria from regexFlag subject regexFlag to regexFlag + maxAge time.Duration } func (*matchCmd) Name() string { @@ -30,7 +33,7 @@ func (*matchCmd) Synopsis() string { } func (*matchCmd) Usage() string { - return `match [options] : + return `match [flags] : output messages matching all specified criteria exit status will be 1 if no matches were found, otherwise 0 ` @@ -42,6 +45,9 @@ func (m *matchCmd) SetFlags(f *flag.FlagSet) { f.Var(&m.from, "from", "From header matching regexp") f.Var(&m.subject, "subject", "Subject header matching regexp") f.Var(&m.to, "to", "To header matching regexp (must match one)") + f.DurationVar( + &m.maxAge, "maxage", 0, + "Matches must have been received in this time frame (ex: \"10s\", \"5m\")") } func (m *matchCmd) Execute( @@ -101,6 +107,11 @@ func (m *matchCmd) Execute( // match returns true if header matches all defined criteria func (m *matchCmd) match(header *client.MessageHeader) bool { + if m.maxAge > 0 { + if time.Since(header.Date) > m.maxAge { + return false + } + } if m.subject.Defined() { if !m.subject.MatchString(header.Subject) { return false diff --git a/cmd/client/mbox.go b/cmd/client/mbox.go index 71e7aaf..cf2b46e 100644 --- a/cmd/client/mbox.go +++ b/cmd/client/mbox.go @@ -24,7 +24,7 @@ func (*mboxCmd) Synopsis() string { } func (*mboxCmd) Usage() string { - return `mbox [options] : + return `mbox [flags] : output mailbox in mbox format ` }