From 821072765547f4e8032cd5656760ce2e38c0ff82 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 1 Jan 2019 16:40:58 +0100 Subject: [PATCH] Change error handling for sort configuration --- mailq/{structure.go => queue.go} | 16 ++++++++-------- mailq/sorting.go | 9 +++++---- main.go | 5 ++++- 3 files changed, 17 insertions(+), 13 deletions(-) rename mailq/{structure.go => queue.go} (100%) diff --git a/mailq/structure.go b/mailq/queue.go similarity index 100% rename from mailq/structure.go rename to mailq/queue.go index d3e6fc6..0aee682 100644 --- a/mailq/structure.go +++ b/mailq/queue.go @@ -4,6 +4,14 @@ import ( "time" ) +type MailQ struct { + NumTotal int + NumActive int + NumHold int + NumDeferred int + Entries []QEntry +} + type QEntry struct { Id string Status string @@ -13,11 +21,3 @@ type QEntry struct { Recipients []string Reason string } - -type MailQ struct { - NumTotal int - NumActive int - NumHold int - NumDeferred int - Entries []QEntry -} diff --git a/mailq/sorting.go b/mailq/sorting.go index 327a82d..614d220 100644 --- a/mailq/sorting.go +++ b/mailq/sorting.go @@ -1,6 +1,7 @@ package mailq import ( + "errors" "fmt" "sort" ) @@ -29,16 +30,16 @@ func isQEntryAttribute(attributeName string) bool { return isValidAttribute } -func (c qEntrySortConfig) By(attributeName string, order string) qEntrySortConfig { +func (c qEntrySortConfig) By(attributeName string, order string) (qEntrySortConfig, error) { if !isQEntryAttribute(attributeName) { - panic(fmt.Sprintf("Invalid sort attribute: '%s' given!", attributeName)) + return c, errors.New(fmt.Sprintf("Invalid sort attribute: '%s' given!", attributeName)) } if order != "ASC" && order != "DESC" { - panic(fmt.Sprintf("Invalid sort order '%s' given!", order)) + return c, errors.New(fmt.Sprintf("Invalid sort order '%s' given!", order)) } newAttributeConfig := qEntryAttributeSortConfig{attribute: attributeName, order: order} c.attributes = append(c.attributes, newAttributeConfig) - return c + return c, nil } func (queue MailQ) Sort(config qEntrySortConfig) { diff --git a/main.go b/main.go index edc756d..4d3bb83 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,10 @@ func showQueue() { fmt.Printf("Could not fetch queue entries: %s\n", err.Error()) os.Exit(1) } - sortConfig := mailq.NewSortConfig().By("Status", "ASC").By("Sender", "ASC").By("Recipients", "DESC") + sortConfig := mailq.NewSortConfig() + sortConfig.By("Status", "ASC") + sortConfig.By("Sender", "ASC") + sortConfig.By("Recipients", "DESC") queue.Sort(sortConfig) if settings["usePager"] == "true" { pagerReader, pagerWriter := io.Pipe()