Improve how sort configuration gets passed around

This commit is contained in:
Jan Philipp Timme 2019-01-01 20:10:34 +01:00
parent 7c33b16b8c
commit b2d1afadfc
3 changed files with 36 additions and 14 deletions

View File

@ -6,7 +6,7 @@ import (
"sort" "sort"
) )
type qEntrySortConfig struct { type QEntrySortConfig struct {
attributes []qEntryAttributeSortConfig attributes []qEntryAttributeSortConfig
} }
@ -15,8 +15,8 @@ type qEntryAttributeSortConfig struct {
order string order string
} }
func NewSortConfig() qEntrySortConfig { func NewSortConfig() QEntrySortConfig {
return qEntrySortConfig{} return QEntrySortConfig{}
} }
func isQEntryAttribute(attributeName string) bool { func isQEntryAttribute(attributeName string) bool {
@ -30,7 +30,7 @@ func isQEntryAttribute(attributeName string) bool {
return isValidAttribute 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) { if !isQEntryAttribute(attributeName) {
return c, errors.New(fmt.Sprintf("Invalid sort attribute: '%s' given!", 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 return c, nil
} }
func (queue MailQ) Sort(config qEntrySortConfig) { func (queue MailQ) Sort(config QEntrySortConfig) {
if len(config.attributes) == 0 { if len(config.attributes) == 0 {
return return
} }

16
main.go
View File

@ -18,7 +18,7 @@ var settings = make(map[string]string)
func main() { func main() {
parseArguments() parseArguments()
if settings["interactive"] == "false" { if settings["interactive"] == "false" {
showQueue() showQueue(defaultSortConfig())
} }
if settings["interactive"] == "true" { if settings["interactive"] == "true" {
interactiveShell(os.Stdin, os.Stdout) interactiveShell(os.Stdin, os.Stdout)
@ -39,16 +39,20 @@ func parseArguments() {
settings["usePager"] = strconv.FormatBool(*usePagerPtr) 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() queue, err := fetchQueue()
if err != nil { if err != nil {
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()
sortConfig, _ = sortConfig.By("Status", "ASC")
sortConfig, _ = sortConfig.By("Sender", "ASC")
sortConfig, _ = 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()

View File

@ -7,11 +7,27 @@ import (
"strings" "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) { func interactiveShell(reader io.Reader, writer io.Writer) {
var state shellState = initialShellState()
fmt.Fprintln(writer, "Let's try an interactive shell!") fmt.Fprintln(writer, "Let's try an interactive shell!")
scanner := bufio.NewScanner(reader) scanner := bufio.NewScanner(reader)
for { for {
var quitShell bool = false
fmt.Fprint(writer, "> ") fmt.Fprint(writer, "> ")
if !scanner.Scan() { if !scanner.Scan() {
break break
@ -28,12 +44,14 @@ func interactiveShell(reader io.Reader, writer io.Writer) {
case "help", "?": case "help", "?":
printHelp(writer) printHelp(writer)
case "exit", "quit": case "exit", "quit":
quitShell = true state.QuitShell = true
case "sort":
fmt.Println("TODO: Implement this!")
default: default:
fmt.Fprintf(writer, "Unknown command '%s'\n", cmd) fmt.Fprintf(writer, "Unknown command '%s'\n", cmd)
fmt.Fprintln(writer, "Use 'help' to display help command") fmt.Fprintln(writer, "Use 'help' to display help command")
} }
if quitShell == true { if state.QuitShell == true {
break break
} }
} }