Change error handling for sort configuration

This commit is contained in:
JPT
2019-01-01 16:40:58 +01:00
parent bd6e9cdea5
commit 8210727655
3 changed files with 17 additions and 13 deletions
+8 -8
View File
@@ -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
}
+5 -4
View File
@@ -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) {
+4 -1
View File
@@ -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()