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"
)
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
}

16
main.go
View File

@ -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()

View File

@ -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
}
}