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 {
|
|
|
|
SortConfig mailq.QEntrySortConfig
|
|
|
|
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 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 19:37:05 +01:00
|
|
|
fmt.Fprintln(writer, "Let's try an interactive shell!")
|
|
|
|
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 19:37:05 +01:00
|
|
|
case "exit", "quit":
|
2019-01-01 20:10:34 +01:00
|
|
|
state.QuitShell = true
|
|
|
|
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
|
|
|
fmt.Fprintln(writer, "\nGoodbye!\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2019-01-01 19:24:43 +01:00
|
|
|
}
|