Allow printing queue data in parseable format

This commit is contained in:
Jan Philipp Timme 2018-12-31 21:08:24 +01:00
parent 7f4e19e285
commit 92a709d196
2 changed files with 22 additions and 8 deletions

28
main.go
View File

@ -30,11 +30,13 @@ func parseArguments() {
mailqCommandArgsPtr := flag.String("mailqCommandArgs", "", "Optional arguments to pass to mailqCommand") mailqCommandArgsPtr := flag.String("mailqCommandArgs", "", "Optional arguments to pass to mailqCommand")
interactivePtr := flag.Bool("interactive", false, "Set to true for shell mode") interactivePtr := flag.Bool("interactive", false, "Set to true for shell mode")
machineFormatPtr := flag.Bool("machineFormat", false, "Set to true to get output suitable for cut/awk/...") machineFormatPtr := flag.Bool("machineFormat", false, "Set to true to get output suitable for cut/awk/...")
usePagerPtr := flag.Bool("usePager", true, "Use pager to display output?")
flag.Parse() flag.Parse()
settings["mailqCommand"] = *mailqCommandPtr settings["mailqCommand"] = *mailqCommandPtr
settings["mailqCommandArgs"] = *mailqCommandArgsPtr settings["mailqCommandArgs"] = *mailqCommandArgsPtr
settings["interactive"] = strconv.FormatBool(*interactivePtr) settings["interactive"] = strconv.FormatBool(*interactivePtr)
settings["machineFormat"] = strconv.FormatBool(*machineFormatPtr) settings["machineFormat"] = strconv.FormatBool(*machineFormatPtr)
settings["usePager"] = strconv.FormatBool(*usePagerPtr)
} }
func interactiveShell() { func interactiveShell() {
@ -47,13 +49,25 @@ 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)
} }
pagerReader, pagerWriter := io.Pipe() if settings["usePager"] == "true" {
pagerDone := make(chan bool) pagerReader, pagerWriter := io.Pipe()
go launchPager(pagerReader, pagerDone) pagerDone := make(chan bool)
queue.PrintHumanReadable(pagerWriter) go launchPager(pagerReader, pagerDone)
pagerWriter.Close() printQueue(queue, pagerWriter)
// Wait for pager to be closed before continuing the main program pagerWriter.Close()
<-pagerDone // Wait for pager to be closed before continuing the main program
<-pagerDone
} else {
printQueue(queue, os.Stdout)
}
}
func printQueue(queue parser.MailQ, writer io.Writer) {
if settings["machineFormat"] == "true" {
queue.PrintMachineReadable(writer)
} else {
queue.PrintHumanReadable(writer)
}
} }
func launchPager(reader *io.PipeReader, pagerDone chan<- bool) { func launchPager(reader *io.PipeReader, pagerDone chan<- bool) {

View File

@ -41,7 +41,7 @@ func (m QEntry) String() string {
} }
func (m QEntry) MachineReadableString() string { func (m QEntry) MachineReadableString() string {
return fmt.Sprintf("%s_%d_%s_%s_%s", m.Id, m.Status, m.Date.Format(SortableDateFormat), m.Size, m.Sender, m.Reason, strings.Join(m.Recipients, ",")) return fmt.Sprintf("%s_%s_%s_%d_%s_%s_%s", m.Id, m.Status, m.Date.Format(SortableDateFormat), m.Size, m.Sender, m.Reason, strings.Join(m.Recipients, ","))
} }
func (m QEntry) DetailedString() string { func (m QEntry) DetailedString() string {