110 lines
3.1 KiB
Go
110 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"strconv"
|
|
"text/tabwriter"
|
|
)
|
|
|
|
import (
|
|
"mailq-inspector/parser"
|
|
)
|
|
|
|
var settings = make(map[string]string)
|
|
|
|
func main() {
|
|
parseArguments()
|
|
if settings["interactive"] == "false" {
|
|
showQueue()
|
|
}
|
|
if settings["interactive"] == "true" {
|
|
interactiveShell()
|
|
}
|
|
}
|
|
|
|
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")
|
|
flag.Parse()
|
|
settings["mailqCommand"] = *mailqCommandPtr
|
|
settings["mailqCommandArgs"] = *mailqCommandArgsPtr
|
|
settings["interactive"] = strconv.FormatBool(*interactivePtr)
|
|
}
|
|
|
|
func interactiveShell() {
|
|
|
|
}
|
|
|
|
func showQueue() {
|
|
queue, err := fetchQueue()
|
|
if err != nil {
|
|
fmt.Printf("Could not fetch queue entries: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
pagerReader, pagerWriter := io.Pipe()
|
|
pagerDone := make(chan bool)
|
|
go launchPager(pagerReader, pagerDone)
|
|
printQueueEntries(queue, pagerWriter)
|
|
pagerWriter.Close()
|
|
// Wait for pager to be closed before continuing the main program
|
|
<-pagerDone
|
|
}
|
|
|
|
func fetchQueue() (parser.MailQ, error) {
|
|
if settings["mailqCommandArgs"] == "" {
|
|
return readFromCmd(exec.Command(settings["mailqCommand"]))
|
|
} else {
|
|
return readFromCmd(exec.Command(settings["mailqCommand"], settings["mailqCommandArgs"]))
|
|
}
|
|
}
|
|
|
|
func printQueueEntries(queue parser.MailQ, writer io.Writer) {
|
|
fmt.Fprintf(writer, "%d entries total (%d active, %d deferred, %d on hold)\n\n", len(queue.Entries), queue.NumActive, queue.NumDeferred, queue.NumHold)
|
|
tabWriter := tabwriter.NewWriter(writer, 2, 2, 1, ' ', tabwriter.TabIndent)
|
|
fmt.Fprintf(tabWriter, "| %s\t| %s\t| %s\t| %s\t| %s\t| %s\t| %s\t| %s\t| \n", "Date", "Id", "Status", "Size", "Sender", "#", "First Recipient", "Reason")
|
|
for _, entry := range queue.Entries {
|
|
_, writeError := fmt.Fprintf(tabWriter, "| %s\t| %s\t| %s\t| %d\t| %s\t| {%d}\t| %s\t| %s\t| \n", entry.Date.Format(parser.SortableDateFormat), entry.Id, entry.Status, entry.Size, entry.Sender, len(entry.Recipients), entry.Recipients[0], entry.Reason)
|
|
if writeError != nil {
|
|
// A writeError is expected once the reader was closed
|
|
// This happens when the pager application got terminated
|
|
break
|
|
}
|
|
}
|
|
tabWriter.Flush()
|
|
}
|
|
|
|
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 readFromCmd(cmd *exec.Cmd) (parser.MailQ, error) {
|
|
stdout, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
return parser.MailQ{}, err
|
|
}
|
|
if err := cmd.Start(); err != nil {
|
|
return parser.MailQ{}, err
|
|
}
|
|
result, resultError := parser.ParseMailQ(stdout)
|
|
if err := cmd.Wait(); err != nil {
|
|
return parser.MailQ{}, err
|
|
}
|
|
return result, resultError
|
|
}
|