mailq-inspector/main.go

121 lines
3.2 KiB
Go

package main
import (
"flag"
"fmt"
"io"
"os"
"os/exec"
"strconv"
)
import (
"mailq-inspector/mailq"
)
var settings = make(map[string]string)
func main() {
parseArguments()
if settings["interactive"] == "false" {
showQueue(defaultSortConfig())
}
if settings["interactive"] == "true" {
interactiveShell(os.Stdin, os.Stdout)
}
}
func parseArguments() {
mailqCommandPtr := flag.String("mailqCommand", "mailq", "Command to use for getting mailq output")
mailqCommandArgsPtr := flag.String("mailqCommandArgs", "", "Optional arguments to pass to mailqCommand")
interactivePtr := flag.Bool("interactive", false, "Set to true for shell mode")
outputFormatPtr := flag.String("outputFormat", "human", "Available output formats: \"human\", \"machine\", \"json\"")
usePagerPtr := flag.Bool("usePager", true, "Use pager to display output?")
flag.Parse()
settings["mailqCommand"] = *mailqCommandPtr
settings["mailqCommandArgs"] = *mailqCommandArgsPtr
settings["interactive"] = strconv.FormatBool(*interactivePtr)
settings["outputFormat"] = *outputFormatPtr
settings["usePager"] = strconv.FormatBool(*usePagerPtr)
}
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)
}
queue.Sort(sortConfig)
displayQueue(queue)
}
func displayQueue(queue mailq.MailQ) {
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 mailq.MailQ, writer io.Writer) {
if settings["outputFormat"] == "human" {
queue.WriteHumanReadable(writer)
} else if settings["outputFormat"] == "machine" {
queue.WriteMachineReadable(writer)
} else if settings["outputFormat"] == "json" {
queue.WriteJSON(writer)
}
}
func launchPager(reader *io.PipeReader, pagerDone chan<- bool) {
cmd := exec.Command("less", "--chop-long-lines")
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 {
reader.Close()
pagerDone <- true
}
}
func fetchQueue() (mailq.MailQ, error) {
if settings["mailqCommandArgs"] == "" {
return readFromCmd(exec.Command(settings["mailqCommand"]))
} else {
return readFromCmd(exec.Command(settings["mailqCommand"], settings["mailqCommandArgs"]))
}
}
func readFromCmd(cmd *exec.Cmd) (mailq.MailQ, error) {
stdout, err := cmd.StdoutPipe()
if err != nil {
return mailq.MailQ{}, err
}
if err := cmd.Start(); err != nil {
return mailq.MailQ{}, err
}
result, resultError := mailq.ParseFromReader(stdout)
if err := cmd.Wait(); err != nil {
return mailq.MailQ{}, err
}
return result, resultError
}