2018-12-29 01:42:50 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-12-29 21:21:03 +01:00
|
|
|
"flag"
|
2018-12-29 18:09:44 +01:00
|
|
|
"fmt"
|
2018-12-30 14:51:08 +01:00
|
|
|
"io"
|
2018-12-29 18:09:44 +01:00
|
|
|
"os"
|
2018-12-29 21:21:03 +01:00
|
|
|
"os/exec"
|
2018-12-29 23:44:40 +01:00
|
|
|
"strconv"
|
2018-12-29 01:42:50 +01:00
|
|
|
)
|
|
|
|
|
2018-12-29 21:22:47 +01:00
|
|
|
import (
|
|
|
|
"mailq-inspector/parser"
|
|
|
|
)
|
|
|
|
|
2018-12-29 21:21:03 +01:00
|
|
|
var settings = make(map[string]string)
|
|
|
|
|
2018-12-29 23:44:40 +01:00
|
|
|
func main() {
|
|
|
|
parseArguments()
|
|
|
|
if settings["interactive"] == "false" {
|
|
|
|
showQueue()
|
|
|
|
}
|
|
|
|
if settings["interactive"] == "true" {
|
|
|
|
interactiveShell()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseArguments() {
|
2018-12-29 22:02:23 +01:00
|
|
|
mailqCommandPtr := flag.String("mailqCommand", "mailq", "Command to use for getting mailq output")
|
|
|
|
mailqCommandArgsPtr := flag.String("mailqCommandArgs", "", "Optional arguments to pass to mailqCommand")
|
2018-12-29 23:44:40 +01:00
|
|
|
interactivePtr := flag.Bool("interactive", false, "Set to true for shell mode")
|
2018-12-31 21:22:23 +01:00
|
|
|
outputFormatPtr := flag.String("outputFormat", "human", "Available output formats: \"human\", \"machine\", \"json\"")
|
2018-12-31 21:08:24 +01:00
|
|
|
usePagerPtr := flag.Bool("usePager", true, "Use pager to display output?")
|
2018-12-29 21:21:03 +01:00
|
|
|
flag.Parse()
|
2018-12-29 22:02:45 +01:00
|
|
|
settings["mailqCommand"] = *mailqCommandPtr
|
|
|
|
settings["mailqCommandArgs"] = *mailqCommandArgsPtr
|
2018-12-29 23:44:40 +01:00
|
|
|
settings["interactive"] = strconv.FormatBool(*interactivePtr)
|
2018-12-31 21:22:23 +01:00
|
|
|
settings["outputFormat"] = *outputFormatPtr
|
2018-12-31 21:08:24 +01:00
|
|
|
settings["usePager"] = strconv.FormatBool(*usePagerPtr)
|
2018-12-29 21:21:03 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 23:44:40 +01:00
|
|
|
func interactiveShell() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func showQueue() {
|
2018-12-31 17:16:32 +01:00
|
|
|
queue, err := fetchQueue()
|
2018-12-31 16:32:23 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Could not fetch queue entries: %s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-01-01 14:36:50 +01:00
|
|
|
sortConfig := parser.NewSortConfig().By("Status", "ASC").By("Sender", "ASC").By("Recipients", "DESC")
|
|
|
|
queue.Sort(sortConfig)
|
2018-12-31 21:08:24 +01:00
|
|
|
if settings["usePager"] == "true" {
|
|
|
|
pagerReader, pagerWriter := io.Pipe()
|
|
|
|
pagerDone := make(chan bool)
|
|
|
|
go launchPager(pagerReader, pagerDone)
|
|
|
|
printQueue(queue, pagerWriter)
|
|
|
|
pagerWriter.Close()
|
|
|
|
// 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) {
|
2018-12-31 21:22:23 +01:00
|
|
|
if settings["outputFormat"] == "human" {
|
2018-12-31 21:08:24 +01:00
|
|
|
queue.PrintHumanReadable(writer)
|
2018-12-31 21:22:23 +01:00
|
|
|
} else if settings["outputFormat"] == "machine" {
|
|
|
|
queue.PrintMachineReadable(writer)
|
|
|
|
} else if settings["outputFormat"] == "json" {
|
|
|
|
queue.PrintJSON(writer)
|
2018-12-31 21:08:24 +01:00
|
|
|
}
|
2018-12-31 16:32:23 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 14:51:08 +01:00
|
|
|
func launchPager(reader *io.PipeReader, pagerDone chan<- bool) {
|
2018-12-31 16:47:53 +01:00
|
|
|
cmd := exec.Command("less", "--chop-long-lines")
|
2018-12-30 14:51:08 +01:00
|
|
|
cmd.Stdin = reader
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
fmt.Printf("Error launching pager: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
fmt.Printf("Error terminating pager: %s\n", err.Error())
|
|
|
|
} else {
|
2018-12-31 15:53:51 +01:00
|
|
|
reader.Close()
|
2018-12-30 14:51:08 +01:00
|
|
|
pagerDone <- true
|
2018-12-29 21:21:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:36:14 +01:00
|
|
|
func fetchQueue() (parser.MailQ, error) {
|
|
|
|
if settings["mailqCommandArgs"] == "" {
|
|
|
|
return readFromCmd(exec.Command(settings["mailqCommand"]))
|
|
|
|
} else {
|
|
|
|
return readFromCmd(exec.Command(settings["mailqCommand"], settings["mailqCommandArgs"]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 17:16:32 +01:00
|
|
|
func readFromCmd(cmd *exec.Cmd) (parser.MailQ, error) {
|
2018-12-29 21:21:03 +01:00
|
|
|
stdout, err := cmd.StdoutPipe()
|
2018-12-29 22:02:45 +01:00
|
|
|
if err != nil {
|
2018-12-31 17:16:32 +01:00
|
|
|
return parser.MailQ{}, err
|
2018-12-29 22:02:45 +01:00
|
|
|
}
|
2018-12-29 22:02:23 +01:00
|
|
|
if err := cmd.Start(); err != nil {
|
2018-12-31 17:16:32 +01:00
|
|
|
return parser.MailQ{}, err
|
2018-12-29 22:02:45 +01:00
|
|
|
}
|
2018-12-29 22:02:23 +01:00
|
|
|
result, resultError := parser.ParseMailQ(stdout)
|
|
|
|
if err := cmd.Wait(); err != nil {
|
2018-12-31 17:16:32 +01:00
|
|
|
return parser.MailQ{}, err
|
2018-12-29 21:21:03 +01:00
|
|
|
}
|
2018-12-29 22:02:23 +01:00
|
|
|
return result, resultError
|
2018-12-29 01:42:50 +01:00
|
|
|
}
|