2018-12-29 01:42:50 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-12-29 21:21:03 +01:00
|
|
|
"flag"
|
2018-12-29 18:09:44 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-12-29 21:21:03 +01:00
|
|
|
"os/exec"
|
2018-12-29 01:42:50 +01:00
|
|
|
)
|
|
|
|
|
2018-12-29 21:22:47 +01:00
|
|
|
import (
|
|
|
|
"mailq-inspector/parser"
|
|
|
|
)
|
|
|
|
|
2018-12-29 21:21:03 +01:00
|
|
|
var settings = make(map[string]string)
|
|
|
|
|
|
|
|
func argParse() {
|
2018-12-29 22:10:53 +01:00
|
|
|
inputModePtr := flag.String("inputMode", "cmd", "Where to read mailq output from. Available options: \"stdin\", \"cmd\"")
|
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 21:21:03 +01:00
|
|
|
flag.Parse()
|
|
|
|
settings["inputMode"] = *inputModePtr
|
2018-12-29 22:02:45 +01:00
|
|
|
settings["mailqCommand"] = *mailqCommandPtr
|
|
|
|
settings["mailqCommandArgs"] = *mailqCommandArgsPtr
|
2018-12-29 21:21:03 +01:00
|
|
|
}
|
|
|
|
|
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" {
|
2018-12-29 22:02:23 +01:00
|
|
|
entries, err = readFromCmd(exec.Command(settings["mailqCommand"]))
|
2018-12-29 21:21:03 +01:00
|
|
|
} 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) {
|
2018-12-29 22:02:23 +01:00
|
|
|
return parser.ParseMailQ(os.Stdin)
|
2018-12-29 21:21:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 21:21:03 +01:00
|
|
|
}
|
2018-12-29 22:02:23 +01:00
|
|
|
return result, resultError
|
2018-12-29 01:42:50 +01:00
|
|
|
}
|