66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
import (
|
|
"mailq-inspector/parser"
|
|
)
|
|
|
|
var settings = make(map[string]string)
|
|
|
|
func argParse() {
|
|
inputModePtr := flag.String("inputMode", "cmd", "Where to read mailq output from. Available options: \"stdin\", \"cmd\"")
|
|
mailqCommandPtr := flag.String("mailqCommand", "mailq", "Command to use for getting mailq output")
|
|
mailqCommandArgsPtr := flag.String("mailqCommandArgs", "", "Optional arguments to pass to mailqCommand")
|
|
flag.Parse()
|
|
settings["inputMode"] = *inputModePtr
|
|
settings["mailqCommand"] = *mailqCommandPtr
|
|
settings["mailqCommandArgs"] = *mailqCommandArgsPtr
|
|
}
|
|
|
|
func main() {
|
|
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(settings["mailqCommand"]))
|
|
} else {
|
|
fmt.Printf("Unsupported inputMode!")
|
|
flag.PrintDefaults()
|
|
os.Exit(1)
|
|
}
|
|
if err != nil {
|
|
fmt.Printf("Error reading: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
for _, entry := range entries {
|
|
fmt.Printf("%s\n", entry.String())
|
|
}
|
|
}
|
|
|
|
func readFromStdin() ([]parser.QEntry, error) {
|
|
return parser.ParseMailQ(os.Stdin)
|
|
}
|
|
|
|
func readFromCmd(cmd *exec.Cmd) ([]parser.QEntry, error) {
|
|
stdout, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := cmd.Start(); err != nil {
|
|
return nil, err
|
|
}
|
|
result, resultError := parser.ParseMailQ(stdout)
|
|
if err := cmd.Wait(); err != nil {
|
|
return nil, err
|
|
}
|
|
return result, resultError
|
|
}
|