From 660c55fcaf07140bb2cb705615718d44f79b34d6 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sat, 29 Dec 2018 21:21:03 +0100 Subject: [PATCH] Add support for reading from mailq directly --- main.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 25aeb4b..c7cb155 100644 --- a/main.go +++ b/main.go @@ -2,22 +2,58 @@ package main import ( "bufio" + "flag" "fmt" "mailq-inspector/parser" "os" + "os/exec" ) +var settings = make(map[string]string) + +func argParse() { + inputModePtr := flag.String("inputMode", "stdin", "How to get mailq output. Available options: \"stdin\", 'cmd'") + flag.Parse() + settings["inputMode"] = *inputModePtr +} + func main() { - // Read data from stdin - scanner := bufio.NewScanner(os.Stdin) - entries, err := parser.ParseMailQ(scanner) - if err != nil { - fmt.Printf("Got error from parser: %s\n", err.Error()) - os.Exit(1) + argParse() + var entries []parser.QEntry + var err error + if settings["inputMode"] == "stdin" { + entries, err = readFromStdin() + } else if settings["inputMode"] == "cmd" { + entries, err = readFromCmd(exec.Command("mailq")) } else { - for _, entry := range entries { - fmt.Printf("%s\n", entry.String()) - } - os.Exit(0) + fmt.Printf("Unsupported inputMode!") + flag.PrintDefaults() + os.Exit(1) + } + + if err != nil { + fmt.Printf("Error reading: %s\n", err.Error()) + os.Exit(1) + } + + for _, line := range entries { + fmt.Printf("Read: %s", line) } } + +func readFromStdin() ([]parser.QEntry, error) { + scanner := bufio.NewScanner(os.Stdin) + return parser.ParseMailQ(scanner) +} + +func readFromCmd(cmd *exec.Cmd) ([]parser.QEntry, error) { + stdout, err := cmd.StdoutPipe() + err = cmd.Start() + scanner := bufio.NewScanner(stdout) + result, resErr := parser.ParseMailQ(scanner) + err = cmd.Wait() + if err != nil { + fmt.Printf("Oh no: %s", err.Error()) + } + return result, resErr +}