mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
Add maxage flag to match subcommand
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/google/subcommands"
|
"github.com/google/subcommands"
|
||||||
"github.com/jhillyerd/inbucket/rest/client"
|
"github.com/jhillyerd/inbucket/rest/client"
|
||||||
@@ -16,9 +17,11 @@ type matchCmd struct {
|
|||||||
output string
|
output string
|
||||||
outFunc func(headers []*client.MessageHeader) error
|
outFunc func(headers []*client.MessageHeader) error
|
||||||
delete bool
|
delete bool
|
||||||
|
// match criteria
|
||||||
from regexFlag
|
from regexFlag
|
||||||
subject regexFlag
|
subject regexFlag
|
||||||
to regexFlag
|
to regexFlag
|
||||||
|
maxAge time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*matchCmd) Name() string {
|
func (*matchCmd) Name() string {
|
||||||
@@ -30,7 +33,7 @@ func (*matchCmd) Synopsis() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (*matchCmd) Usage() string {
|
func (*matchCmd) Usage() string {
|
||||||
return `match [options] <mailbox>:
|
return `match [flags] <mailbox>:
|
||||||
output messages matching all specified criteria
|
output messages matching all specified criteria
|
||||||
exit status will be 1 if no matches were found, otherwise 0
|
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.from, "from", "From header matching regexp")
|
||||||
f.Var(&m.subject, "subject", "Subject 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.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(
|
func (m *matchCmd) Execute(
|
||||||
@@ -101,6 +107,11 @@ func (m *matchCmd) Execute(
|
|||||||
|
|
||||||
// match returns true if header matches all defined criteria
|
// match returns true if header matches all defined criteria
|
||||||
func (m *matchCmd) match(header *client.MessageHeader) bool {
|
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.Defined() {
|
||||||
if !m.subject.MatchString(header.Subject) {
|
if !m.subject.MatchString(header.Subject) {
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func (*mboxCmd) Synopsis() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (*mboxCmd) Usage() string {
|
func (*mboxCmd) Usage() string {
|
||||||
return `mbox [options] <mailbox>:
|
return `mbox [flags] <mailbox>:
|
||||||
output mailbox in mbox format
|
output mailbox in mbox format
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user