mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
feature: Context support for REST client (#496)
* rest/client: add WithContext methods Signed-off-by: James Hillyerd <james@hillyerd.com> * cmd/client: pass context Signed-off-by: James Hillyerd <james@hillyerd.com> --------- Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
@@ -28,18 +28,20 @@ func (*listCmd) Usage() string {
|
||||
func (l *listCmd) SetFlags(f *flag.FlagSet) {}
|
||||
|
||||
func (l *listCmd) Execute(
|
||||
_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
mailbox := f.Arg(0)
|
||||
if mailbox == "" {
|
||||
return usage("mailbox required")
|
||||
}
|
||||
|
||||
// Setup rest client
|
||||
c, err := client.New(baseURL())
|
||||
if err != nil {
|
||||
return fatal("Couldn't build client", err)
|
||||
}
|
||||
|
||||
// Get list
|
||||
headers, err := c.ListMailbox(mailbox)
|
||||
headers, err := c.ListMailboxWithContext(ctx, mailbox)
|
||||
if err != nil {
|
||||
return fatal("REST call failed", err)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
type matchCmd struct {
|
||||
output string
|
||||
outFunc func(headers []*client.MessageHeader) error
|
||||
outFunc func(ctx context.Context, headers []*client.MessageHeader) error
|
||||
delete bool
|
||||
// match criteria
|
||||
from regexFlag
|
||||
@@ -51,11 +51,12 @@ func (m *matchCmd) SetFlags(f *flag.FlagSet) {
|
||||
}
|
||||
|
||||
func (m *matchCmd) Execute(
|
||||
_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
mailbox := f.Arg(0)
|
||||
if mailbox == "" {
|
||||
return usage("mailbox required")
|
||||
}
|
||||
|
||||
// Select output function
|
||||
switch m.output {
|
||||
case "id":
|
||||
@@ -67,16 +68,19 @@ func (m *matchCmd) Execute(
|
||||
default:
|
||||
return usage("unknown output type: " + m.output)
|
||||
}
|
||||
|
||||
// Setup REST client
|
||||
c, err := client.New(baseURL())
|
||||
if err != nil {
|
||||
return fatal("Couldn't build client", err)
|
||||
}
|
||||
|
||||
// Get list
|
||||
headers, err := c.ListMailbox(mailbox)
|
||||
headers, err := c.ListMailboxWithContext(ctx, mailbox)
|
||||
if err != nil {
|
||||
return fatal("List REST call failed", err)
|
||||
}
|
||||
|
||||
// Find matches
|
||||
matches := make([]*client.MessageHeader, 0, len(headers))
|
||||
for _, h := range headers {
|
||||
@@ -84,24 +88,28 @@ func (m *matchCmd) Execute(
|
||||
matches = append(matches, h)
|
||||
}
|
||||
}
|
||||
|
||||
// Return error status if no matches
|
||||
if len(matches) == 0 {
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
// Output matches
|
||||
err = m.outFunc(matches)
|
||||
err = m.outFunc(ctx, matches)
|
||||
if err != nil {
|
||||
return fatal("Error", err)
|
||||
}
|
||||
|
||||
// Optionally, delete matches
|
||||
if m.delete {
|
||||
// Delete matches
|
||||
for _, h := range matches {
|
||||
err = h.Delete()
|
||||
err = h.DeleteWithContext(ctx)
|
||||
if err != nil {
|
||||
return fatal("Delete REST call failed", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
@@ -148,14 +156,14 @@ func (m *matchCmd) match(header *client.MessageHeader) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func outputID(headers []*client.MessageHeader) error {
|
||||
func outputID(_ context.Context, headers []*client.MessageHeader) error {
|
||||
for _, h := range headers {
|
||||
fmt.Println(h.ID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func outputJSON(headers []*client.MessageHeader) error {
|
||||
func outputJSON(_ context.Context, headers []*client.MessageHeader) error {
|
||||
jsonEncoder := json.NewEncoder(os.Stdout)
|
||||
jsonEncoder.SetEscapeHTML(false)
|
||||
jsonEncoder.SetIndent("", " ")
|
||||
|
||||
@@ -33,42 +33,46 @@ func (m *mboxCmd) SetFlags(f *flag.FlagSet) {
|
||||
}
|
||||
|
||||
func (m *mboxCmd) Execute(
|
||||
_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
mailbox := f.Arg(0)
|
||||
if mailbox == "" {
|
||||
return usage("mailbox required")
|
||||
}
|
||||
|
||||
// Setup REST client
|
||||
c, err := client.New(baseURL())
|
||||
if err != nil {
|
||||
return fatal("Couldn't build client", err)
|
||||
}
|
||||
|
||||
// Get list
|
||||
headers, err := c.ListMailbox(mailbox)
|
||||
headers, err := c.ListMailboxWithContext(ctx, mailbox)
|
||||
if err != nil {
|
||||
return fatal("List REST call failed", err)
|
||||
}
|
||||
err = outputMbox(headers)
|
||||
err = outputMbox(ctx, headers)
|
||||
if err != nil {
|
||||
return fatal("Error", err)
|
||||
}
|
||||
|
||||
// Optionally, delete retrieved messages
|
||||
if m.delete {
|
||||
// Delete matches
|
||||
for _, h := range headers {
|
||||
err = h.Delete()
|
||||
err = h.DeleteWithContext(ctx)
|
||||
if err != nil {
|
||||
return fatal("Delete REST call failed", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
// outputMbox renders messages in mbox format
|
||||
// also used by match subcommand
|
||||
func outputMbox(headers []*client.MessageHeader) error {
|
||||
// outputMbox renders messages in mbox format.
|
||||
// It is also used by match subcommand.
|
||||
func outputMbox(ctx context.Context, headers []*client.MessageHeader) error {
|
||||
for _, h := range headers {
|
||||
source, err := h.GetSource()
|
||||
source, err := h.GetSourceWithContext(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get source REST failed: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user