From 47f133f4189da2b80d2dca7af526eac88086fc1a Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 1 Jan 2019 20:50:39 +0100 Subject: [PATCH] Introduce first basic functions in shell --- mailq/string.go | 15 +++++++++++---- main.go | 4 ++++ shell.go | 39 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/mailq/string.go b/mailq/string.go index 56e8eba..d3963bd 100644 --- a/mailq/string.go +++ b/mailq/string.go @@ -37,10 +37,9 @@ func (queue MailQ) WriteMachineReadable(writer io.Writer) { } func (queue MailQ) WriteHumanReadable(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\n", len(queue.Entries), queue.NumActive, queue.NumDeferred, queue.NumHold) + queue.WriteSummary(writer) + if len(queue.Entries) != 0 { + fmt.Fprintf(writer, "\n") 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") 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) { bytes, err := json.Marshal(queue) if err != nil { diff --git a/main.go b/main.go index 75863f7..153a272 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,10 @@ func showQueue(sortConfig mailq.QEntrySortConfig) { os.Exit(1) } queue.Sort(sortConfig) + displayQueue(queue) +} + +func displayQueue(queue mailq.MailQ) { if settings["usePager"] == "true" { pagerReader, pagerWriter := io.Pipe() pagerDone := make(chan bool) diff --git a/shell.go b/shell.go index f9cc752..ae736bb 100644 --- a/shell.go +++ b/shell.go @@ -14,12 +14,37 @@ import ( type shellState struct { SortConfig mailq.QEntrySortConfig QuitShell bool + Queue mailq.MailQ } func initialShellState() shellState { var state shellState = shellState{} state.QuitShell = false 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 } @@ -30,6 +55,7 @@ func interactiveShell(reader io.Reader, writer io.Writer) { for { fmt.Fprint(writer, "> ") if !scanner.Scan() { + fmt.Println() break } text := strings.Trim(scanner.Text(), " ") @@ -38,15 +64,22 @@ func interactiveShell(reader io.Reader, writer io.Writer) { continue } cmd := textFields[0] - fmt.Fprintf(writer, "DEBUG - Read input: %q\n", textFields) - switch cmd { case "help", "?": printHelp(writer) case "exit", "quit": state.QuitShell = true 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: fmt.Fprintf(writer, "Unknown command '%s'\n", cmd) fmt.Fprintln(writer, "Use 'help' to display help command")