Add basic inspect feature

This commit is contained in:
Jan Philipp Timme 2019-01-01 21:11:39 +01:00
parent 47f133f418
commit f1a6259256
2 changed files with 29 additions and 2 deletions

View File

@ -1,6 +1,8 @@
package mailq package mailq
import ( import (
"errors"
"fmt"
"time" "time"
) )
@ -21,3 +23,12 @@ type QEntry struct {
Recipients []string Recipients []string
Reason 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))
}

View File

@ -48,6 +48,20 @@ func updateSortConfig(fields []string, state shellState) shellState {
return state 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) { func interactiveShell(reader io.Reader, writer io.Writer) {
var state shellState = initialShellState() var state shellState = initialShellState()
fmt.Fprintln(writer, "Let's try an interactive shell!") fmt.Fprintln(writer, "Let's try an interactive shell!")
@ -67,8 +81,10 @@ func interactiveShell(reader io.Reader, writer io.Writer) {
switch cmd { switch cmd {
case "help", "?": case "help", "?":
printHelp(writer) printHelp(writer)
case "exit", "quit": case "exit":
state.QuitShell = true state.QuitShell = true
case "inspect":
displayMessageDetails(textFields, writer, state)
case "sort": case "sort":
state = updateSortConfig(textFields, state) state = updateSortConfig(textFields, state)
case "show": case "show":
@ -94,5 +110,5 @@ func interactiveShell(reader io.Reader, writer io.Writer) {
func printHelp(writer io.Writer) { func printHelp(writer io.Writer) {
fmt.Fprintln(writer, "Available commands:") fmt.Fprintln(writer, "Available commands:")
fmt.Fprintln(writer, "?, help: display this output") fmt.Fprintln(writer, "?, help: display this output")
fmt.Fprintln(writer, "exit, quit: quit this program") fmt.Fprintln(writer, "exit: quit this program")
} }