Change error handling for sort configuration

This commit is contained in:
Jan Philipp Timme 2019-01-01 16:40:58 +01:00
parent bd6e9cdea5
commit 8210727655
3 changed files with 17 additions and 13 deletions

View File

@ -4,6 +4,14 @@ import (
"time" "time"
) )
type MailQ struct {
NumTotal int
NumActive int
NumHold int
NumDeferred int
Entries []QEntry
}
type QEntry struct { type QEntry struct {
Id string Id string
Status string Status string
@ -13,11 +21,3 @@ type QEntry struct {
Recipients []string Recipients []string
Reason string Reason string
} }
type MailQ struct {
NumTotal int
NumActive int
NumHold int
NumDeferred int
Entries []QEntry
}

View File

@ -1,6 +1,7 @@
package mailq package mailq
import ( import (
"errors"
"fmt" "fmt"
"sort" "sort"
) )
@ -29,16 +30,16 @@ func isQEntryAttribute(attributeName string) bool {
return isValidAttribute return isValidAttribute
} }
func (c qEntrySortConfig) By(attributeName string, order string) qEntrySortConfig { func (c qEntrySortConfig) By(attributeName string, order string) (qEntrySortConfig, error) {
if !isQEntryAttribute(attributeName) { 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" { 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} newAttributeConfig := qEntryAttributeSortConfig{attribute: attributeName, order: order}
c.attributes = append(c.attributes, newAttributeConfig) c.attributes = append(c.attributes, newAttributeConfig)
return c return c, nil
} }
func (queue MailQ) Sort(config qEntrySortConfig) { func (queue MailQ) Sort(config qEntrySortConfig) {

View File

@ -49,7 +49,10 @@ func showQueue() {
fmt.Printf("Could not fetch queue entries: %s\n", err.Error()) fmt.Printf("Could not fetch queue entries: %s\n", err.Error())
os.Exit(1) 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) queue.Sort(sortConfig)
if settings["usePager"] == "true" { if settings["usePager"] == "true" {
pagerReader, pagerWriter := io.Pipe() pagerReader, pagerWriter := io.Pipe()