From b2d1afadfc7baf8b2f8aea424bbdb22cf9e94b1e Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 1 Jan 2019 20:10:34 +0100 Subject: [PATCH] Improve how sort configuration gets passed around --- mailq/sorting.go | 10 +++++----- main.go | 16 ++++++++++------ shell.go | 24 +++++++++++++++++++++--- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/mailq/sorting.go b/mailq/sorting.go index 614d220..1df4919 100644 --- a/mailq/sorting.go +++ b/mailq/sorting.go @@ -6,7 +6,7 @@ import ( "sort" ) -type qEntrySortConfig struct { +type QEntrySortConfig struct { attributes []qEntryAttributeSortConfig } @@ -15,8 +15,8 @@ type qEntryAttributeSortConfig struct { order string } -func NewSortConfig() qEntrySortConfig { - return qEntrySortConfig{} +func NewSortConfig() QEntrySortConfig { + return QEntrySortConfig{} } func isQEntryAttribute(attributeName string) bool { @@ -30,7 +30,7 @@ func isQEntryAttribute(attributeName string) bool { return isValidAttribute } -func (c qEntrySortConfig) By(attributeName string, order string) (qEntrySortConfig, error) { +func (c QEntrySortConfig) By(attributeName string, order string) (QEntrySortConfig, error) { if !isQEntryAttribute(attributeName) { return c, errors.New(fmt.Sprintf("Invalid sort attribute: '%s' given!", attributeName)) } @@ -42,7 +42,7 @@ func (c qEntrySortConfig) By(attributeName string, order string) (qEntrySortConf return c, nil } -func (queue MailQ) Sort(config qEntrySortConfig) { +func (queue MailQ) Sort(config QEntrySortConfig) { if len(config.attributes) == 0 { return } diff --git a/main.go b/main.go index 6cac199..75863f7 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ var settings = make(map[string]string) func main() { parseArguments() if settings["interactive"] == "false" { - showQueue() + showQueue(defaultSortConfig()) } if settings["interactive"] == "true" { interactiveShell(os.Stdin, os.Stdout) @@ -39,16 +39,20 @@ func parseArguments() { settings["usePager"] = strconv.FormatBool(*usePagerPtr) } -func showQueue() { +func defaultSortConfig() mailq.QEntrySortConfig { + sortConfig := mailq.NewSortConfig() + sortConfig, _ = sortConfig.By("Status", "ASC") + sortConfig, _ = sortConfig.By("Sender", "ASC") + sortConfig, _ = sortConfig.By("Recipients", "DESC") + return sortConfig +} + +func showQueue(sortConfig mailq.QEntrySortConfig) { queue, err := fetchQueue() if err != nil { fmt.Printf("Could not fetch queue entries: %s\n", err.Error()) os.Exit(1) } - sortConfig := mailq.NewSortConfig() - sortConfig, _ = sortConfig.By("Status", "ASC") - sortConfig, _ = sortConfig.By("Sender", "ASC") - sortConfig, _ = sortConfig.By("Recipients", "DESC") queue.Sort(sortConfig) if settings["usePager"] == "true" { pagerReader, pagerWriter := io.Pipe() diff --git a/shell.go b/shell.go index a79f353..f9cc752 100644 --- a/shell.go +++ b/shell.go @@ -7,11 +7,27 @@ import ( "strings" ) +import ( + "mailq-inspector/mailq" +) + +type shellState struct { + SortConfig mailq.QEntrySortConfig + QuitShell bool +} + +func initialShellState() shellState { + var state shellState = shellState{} + state.QuitShell = false + state.SortConfig = defaultSortConfig() + return state +} + func interactiveShell(reader io.Reader, writer io.Writer) { + var state shellState = initialShellState() fmt.Fprintln(writer, "Let's try an interactive shell!") scanner := bufio.NewScanner(reader) for { - var quitShell bool = false fmt.Fprint(writer, "> ") if !scanner.Scan() { break @@ -28,12 +44,14 @@ func interactiveShell(reader io.Reader, writer io.Writer) { case "help", "?": printHelp(writer) case "exit", "quit": - quitShell = true + state.QuitShell = true + case "sort": + fmt.Println("TODO: Implement this!") default: fmt.Fprintf(writer, "Unknown command '%s'\n", cmd) fmt.Fprintln(writer, "Use 'help' to display help command") } - if quitShell == true { + if state.QuitShell == true { break } }