mailq-inspector/main.go

114 lines
3.0 KiB
Go
Raw Normal View History

2018-12-29 01:42:50 +01:00
package main
import (
"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"
"os/exec"
2018-12-29 23:44:40 +01:00
"strconv"
"text/tabwriter"
2018-12-29 01:42:50 +01:00
)
2018-12-29 21:22:47 +01:00
import (
"mailq-inspector/parser"
)
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()
}
2018-12-30 14:51:08 +01:00
fmt.Printf("I am done.\n")
2018-12-29 23:44:40 +01:00
}
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")
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-29 23:44:40 +01:00
func interactiveShell() {
}
func showQueue() {
entries, err := fetchQueueEntries()
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(entries, pagerWriter)
pagerWriter.Close()
// Wait for pager to be closed before continuing the main program
<-pagerDone
}
func fetchQueueEntries() ([]parser.QEntry, error) {
var entries []parser.QEntry
var err error
2018-12-30 13:34:45 +01:00
if settings["mailqCommandArgs"] == "" {
entries, err = readFromCmd(exec.Command(settings["mailqCommand"]))
} else {
entries, err = readFromCmd(exec.Command(settings["mailqCommand"], settings["mailqCommandArgs"]))
}
return entries, err
}
func printQueueEntries(entries []parser.QEntry, writer io.Writer) {
tabWriter := tabwriter.NewWriter(writer, 0, 2, 1, ' ', tabwriter.TabIndent)
fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\t%s\n", "Date", "Id", "Size", "Sender", "#", "First Recipient")
for _, entry := range entries {
_, writeError := fmt.Fprintf(tabWriter, "%s\t%s\t%d\t%s\t{%d}\t%s\n", entry.Date, entry.Id, entry.Size, entry.Sender, len(entry.Recipients), entry.Recipients[0])
if writeError != nil {
// A writeError is expected once the reader was closed
// This happens when the pager application got terminated
fmt.Printf("Error writing!: %s\n", writeError.Error())
break
}
2018-12-30 14:51:08 +01:00
}
tabWriter.Flush()
2018-12-30 14:51:08 +01:00
}
func launchPager(reader *io.PipeReader, pagerDone chan<- bool) {
cmd := exec.Command("less")
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()
2018-12-30 14:51:08 +01:00
pagerDone <- true
}
}
func readFromCmd(cmd *exec.Cmd) ([]parser.QEntry, error) {
stdout, err := cmd.StdoutPipe()
2018-12-29 22:02:45 +01:00
if err != nil {
return nil, err
}
2018-12-29 22:02:23 +01:00
if err := cmd.Start(); err != nil {
2018-12-29 22:02:45 +01:00
return nil, err
}
2018-12-29 22:02:23 +01:00
result, resultError := parser.ParseMailQ(stdout)
if err := cmd.Wait(); err != nil {
2018-12-29 22:02:45 +01:00
return nil, err
}
2018-12-29 22:02:23 +01:00
return result, resultError
2018-12-29 01:42:50 +01:00
}