2018-12-29 01:42:50 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-12-29 18:09:44 +01:00
|
|
|
"bufio"
|
2018-12-29 21:21:03 +01:00
|
|
|
"flag"
|
2018-12-29 18:09:44 +01:00
|
|
|
"fmt"
|
|
|
|
"mailq-inspector/parser"
|
|
|
|
"os"
|
2018-12-29 21:21:03 +01:00
|
|
|
"os/exec"
|
2018-12-29 01:42:50 +01:00
|
|
|
)
|
|
|
|
|
2018-12-29 21:21:03 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-29 01:42:50 +01:00
|
|
|
func main() {
|
2018-12-29 21:21:03 +01:00
|
|
|
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 {
|
|
|
|
fmt.Printf("Unsupported inputMode!")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:09:44 +01:00
|
|
|
if err != nil {
|
2018-12-29 21:21:03 +01:00
|
|
|
fmt.Printf("Error reading: %s\n", err.Error())
|
2018-12-29 18:09:44 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-12-29 21:21:03 +01:00
|
|
|
|
|
|
|
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
|
2018-12-29 01:42:50 +01:00
|
|
|
}
|