From f1a625925672144c325a0b20f835a7de9a47336c Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 1 Jan 2019 21:11:39 +0100 Subject: [PATCH] Add basic inspect feature --- mailq/queue.go | 11 +++++++++++ shell.go | 20 ++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/mailq/queue.go b/mailq/queue.go index 0aee682..2f29e93 100644 --- a/mailq/queue.go +++ b/mailq/queue.go @@ -1,6 +1,8 @@ package mailq import ( + "errors" + "fmt" "time" ) @@ -21,3 +23,12 @@ type QEntry struct { Recipients []string Reason string } + +func (q MailQ) GetMessageById(id string) (QEntry, error) { + for _, entry := range q.Entries { + if entry.Id == id { + return entry, nil + } + } + return QEntry{}, errors.New(fmt.Sprintf("No message with id '%s' available!", id)) +} diff --git a/shell.go b/shell.go index ae736bb..52b9f92 100644 --- a/shell.go +++ b/shell.go @@ -48,6 +48,20 @@ func updateSortConfig(fields []string, state shellState) shellState { return state } +func displayMessageDetails(fields []string, writer io.Writer, state shellState) { + if len(fields) < 2 { + fmt.Fprintln(writer, "Please state the Id of the message to inspect!") + return + } + messageId := fields[1] + message, err := state.Queue.GetMessageById(messageId) + if err != nil { + fmt.Fprintf(writer, "Could not inspect message with id '%s': '%s'\n", messageId, err.Error()) + } else { + fmt.Fprintf(writer, "%s\n", message.DetailedString()) + } +} + func interactiveShell(reader io.Reader, writer io.Writer) { var state shellState = initialShellState() fmt.Fprintln(writer, "Let's try an interactive shell!") @@ -67,8 +81,10 @@ func interactiveShell(reader io.Reader, writer io.Writer) { switch cmd { case "help", "?": printHelp(writer) - case "exit", "quit": + case "exit": state.QuitShell = true + case "inspect": + displayMessageDetails(textFields, writer, state) case "sort": state = updateSortConfig(textFields, state) case "show": @@ -94,5 +110,5 @@ func interactiveShell(reader io.Reader, writer io.Writer) { func printHelp(writer io.Writer) { fmt.Fprintln(writer, "Available commands:") fmt.Fprintln(writer, "?, help: display this output") - fmt.Fprintln(writer, "exit, quit: quit this program") + fmt.Fprintln(writer, "exit: quit this program") }