Add support for reading from mailq directly

This commit is contained in:
Jan Philipp Timme 2018-12-29 21:21:03 +01:00
parent 063fb5756d
commit 660c55fcaf

54
main.go
View File

@ -2,22 +2,58 @@ package main
import ( import (
"bufio" "bufio"
"flag"
"fmt" "fmt"
"mailq-inspector/parser" "mailq-inspector/parser"
"os" "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() { func main() {
// Read data from stdin argParse()
scanner := bufio.NewScanner(os.Stdin) var entries []parser.QEntry
entries, err := parser.ParseMailQ(scanner) var err error
if err != nil { if settings["inputMode"] == "stdin" {
fmt.Printf("Got error from parser: %s\n", err.Error()) entries, err = readFromStdin()
os.Exit(1) } else if settings["inputMode"] == "cmd" {
entries, err = readFromCmd(exec.Command("mailq"))
} else { } else {
for _, entry := range entries { fmt.Printf("Unsupported inputMode!")
fmt.Printf("%s\n", entry.String()) flag.PrintDefaults()
os.Exit(1)
} }
os.Exit(0)
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
}