2019-01-01 19:24:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2019-01-01 19:37:05 +01:00
|
|
|
"io"
|
2019-01-01 19:24:43 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-01-01 20:10:34 +01:00
|
|
|
import (
|
|
|
|
"mailq-inspector/mailq"
|
|
|
|
)
|
|
|
|
|
|
|
|
type shellState struct {
|
2019-01-01 21:23:04 +01:00
|
|
|
SortConfig mailq.QSortConfig
|
2019-01-01 20:10:34 +01:00
|
|
|
QuitShell bool
|
2019-01-01 20:50:39 +01:00
|
|
|
Queue mailq.MailQ
|
2019-01-01 20:10:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func initialShellState() shellState {
|
|
|
|
var state shellState = shellState{}
|
|
|
|
state.QuitShell = false
|
|
|
|
state.SortConfig = defaultSortConfig()
|
2019-01-01 20:50:39 +01:00
|
|
|
state = refreshQueue(state)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshQueue(state shellState) shellState {
|
|
|
|
queue, err := fetchQueue()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error fetching queue: '%s'\n", err.Error())
|
|
|
|
}
|
|
|
|
state.Queue = queue
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSortConfig(fields []string, state shellState) shellState {
|
|
|
|
sortConfig := mailq.NewSortConfig()
|
|
|
|
var err error
|
|
|
|
for _, field := range fields[1:] {
|
|
|
|
split := strings.Split(field, ":")
|
|
|
|
sortConfig, err = sortConfig.By(split[0], split[1])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error applying sort config: '%s'\n", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.SortConfig = sortConfig
|
2019-01-01 20:10:34 +01:00
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2019-01-01 21:11:39 +01:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-01 19:37:05 +01:00
|
|
|
func interactiveShell(reader io.Reader, writer io.Writer) {
|
2019-01-01 20:10:34 +01:00
|
|
|
var state shellState = initialShellState()
|
2019-01-01 21:16:06 +01:00
|
|
|
printHelp(writer)
|
2019-01-01 19:37:05 +01:00
|
|
|
scanner := bufio.NewScanner(reader)
|
2019-01-01 19:24:43 +01:00
|
|
|
for {
|
2019-01-01 19:37:05 +01:00
|
|
|
fmt.Fprint(writer, "> ")
|
2019-01-01 19:24:43 +01:00
|
|
|
if !scanner.Scan() {
|
2019-01-01 20:50:39 +01:00
|
|
|
fmt.Println()
|
2019-01-01 19:24:43 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
text := strings.Trim(scanner.Text(), " ")
|
|
|
|
textFields := strings.Fields(text)
|
|
|
|
if len(textFields) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmd := textFields[0]
|
|
|
|
switch cmd {
|
2019-01-01 19:37:05 +01:00
|
|
|
case "help", "?":
|
2019-01-01 19:42:22 +01:00
|
|
|
printHelp(writer)
|
2019-01-01 21:11:39 +01:00
|
|
|
case "exit":
|
2019-01-01 20:10:34 +01:00
|
|
|
state.QuitShell = true
|
2019-01-01 21:11:39 +01:00
|
|
|
case "inspect":
|
|
|
|
displayMessageDetails(textFields, writer, state)
|
2019-01-01 20:10:34 +01:00
|
|
|
case "sort":
|
2019-01-01 20:50:39 +01:00
|
|
|
state = updateSortConfig(textFields, state)
|
|
|
|
case "show":
|
|
|
|
state.Queue.Sort(state.SortConfig)
|
|
|
|
displayQueue(state.Queue)
|
|
|
|
case "summary":
|
|
|
|
state.Queue.WriteSummary(writer)
|
|
|
|
case "reload":
|
|
|
|
state = refreshQueue(state)
|
|
|
|
fmt.Fprintln(writer, "Queue data reloaded")
|
|
|
|
state.Queue.WriteSummary(writer)
|
2019-01-01 19:24:43 +01:00
|
|
|
default:
|
2019-01-01 19:37:05 +01:00
|
|
|
fmt.Fprintf(writer, "Unknown command '%s'\n", cmd)
|
|
|
|
fmt.Fprintln(writer, "Use 'help' to display help command")
|
|
|
|
}
|
2019-01-01 20:10:34 +01:00
|
|
|
if state.QuitShell == true {
|
2019-01-01 19:37:05 +01:00
|
|
|
break
|
2019-01-01 19:24:43 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-01 19:37:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func printHelp(writer io.Writer) {
|
|
|
|
fmt.Fprintln(writer, "Available commands:")
|
2019-01-01 21:16:06 +01:00
|
|
|
fmt.Fprintln(writer, "help\tdisplay this output")
|
|
|
|
fmt.Fprintln(writer, "exit\tquit this program")
|
|
|
|
fmt.Fprintln(writer, "reload\treload mailq")
|
|
|
|
fmt.Fprintln(writer, "sort\tdefine how to sort mailq before showing")
|
|
|
|
fmt.Fprintln(writer, "show\tshow mailq")
|
|
|
|
fmt.Fprintln(writer, "summary\tshow mailq summary")
|
|
|
|
fmt.Fprintln(writer, "")
|
2019-01-01 19:24:43 +01:00
|
|
|
}
|