Introduce first basic functions in shell

This commit is contained in:
Jan Philipp Timme 2019-01-01 20:50:39 +01:00
parent b2d1afadfc
commit 47f133f418
3 changed files with 51 additions and 7 deletions

View File

@ -37,10 +37,9 @@ func (queue MailQ) WriteMachineReadable(writer io.Writer) {
} }
func (queue MailQ) WriteHumanReadable(writer io.Writer) { func (queue MailQ) WriteHumanReadable(writer io.Writer) {
if len(queue.Entries) == 0 { queue.WriteSummary(writer)
fmt.Fprintf(writer, "Mail queue is empty\n") if len(queue.Entries) != 0 {
} else { fmt.Fprintf(writer, "\n")
fmt.Fprintf(writer, "%d entries total (%d active, %d deferred, %d on hold)\n\n", len(queue.Entries), queue.NumActive, queue.NumDeferred, queue.NumHold)
tabWriter := tabwriter.NewWriter(writer, 2, 2, 1, ' ', tabwriter.TabIndent) tabWriter := tabwriter.NewWriter(writer, 2, 2, 1, ' ', tabwriter.TabIndent)
fmt.Fprintf(tabWriter, "| %s\t| %s\t| %s\t| %s\t| %s\t| %s\t| %s\t| %s\t| \n", "Date", "Id", "Status", "Size", "Sender", "#", "First Recipient", "Reason") fmt.Fprintf(tabWriter, "| %s\t| %s\t| %s\t| %s\t| %s\t| %s\t| %s\t| %s\t| \n", "Date", "Id", "Status", "Size", "Sender", "#", "First Recipient", "Reason")
for _, entry := range queue.Entries { for _, entry := range queue.Entries {
@ -54,6 +53,14 @@ func (queue MailQ) WriteHumanReadable(writer io.Writer) {
} }
} }
func (queue MailQ) WriteSummary(writer io.Writer) {
if len(queue.Entries) == 0 {
fmt.Fprintf(writer, "Mail queue is empty\n")
} else {
fmt.Fprintf(writer, "%d entries total (%d active, %d deferred, %d on hold)\n", len(queue.Entries), queue.NumActive, queue.NumDeferred, queue.NumHold)
}
}
func (queue MailQ) WriteJSON(writer io.Writer) { func (queue MailQ) WriteJSON(writer io.Writer) {
bytes, err := json.Marshal(queue) bytes, err := json.Marshal(queue)
if err != nil { if err != nil {

View File

@ -54,6 +54,10 @@ func showQueue(sortConfig mailq.QEntrySortConfig) {
os.Exit(1) os.Exit(1)
} }
queue.Sort(sortConfig) queue.Sort(sortConfig)
displayQueue(queue)
}
func displayQueue(queue mailq.MailQ) {
if settings["usePager"] == "true" { if settings["usePager"] == "true" {
pagerReader, pagerWriter := io.Pipe() pagerReader, pagerWriter := io.Pipe()
pagerDone := make(chan bool) pagerDone := make(chan bool)

View File

@ -14,12 +14,37 @@ import (
type shellState struct { type shellState struct {
SortConfig mailq.QEntrySortConfig SortConfig mailq.QEntrySortConfig
QuitShell bool QuitShell bool
Queue mailq.MailQ
} }
func initialShellState() shellState { func initialShellState() shellState {
var state shellState = shellState{} var state shellState = shellState{}
state.QuitShell = false state.QuitShell = false
state.SortConfig = defaultSortConfig() state.SortConfig = defaultSortConfig()
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
return state return state
} }
@ -30,6 +55,7 @@ func interactiveShell(reader io.Reader, writer io.Writer) {
for { for {
fmt.Fprint(writer, "> ") fmt.Fprint(writer, "> ")
if !scanner.Scan() { if !scanner.Scan() {
fmt.Println()
break break
} }
text := strings.Trim(scanner.Text(), " ") text := strings.Trim(scanner.Text(), " ")
@ -38,15 +64,22 @@ func interactiveShell(reader io.Reader, writer io.Writer) {
continue continue
} }
cmd := textFields[0] cmd := textFields[0]
fmt.Fprintf(writer, "DEBUG - Read input: %q\n", textFields)
switch cmd { switch cmd {
case "help", "?": case "help", "?":
printHelp(writer) printHelp(writer)
case "exit", "quit": case "exit", "quit":
state.QuitShell = true state.QuitShell = true
case "sort": case "sort":
fmt.Println("TODO: Implement this!") 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)
default: default:
fmt.Fprintf(writer, "Unknown command '%s'\n", cmd) fmt.Fprintf(writer, "Unknown command '%s'\n", cmd)
fmt.Fprintln(writer, "Use 'help' to display help command") fmt.Fprintln(writer, "Use 'help' to display help command")