72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"strconv"
|
|
)
|
|
|
|
import (
|
|
"mailq-inspector/parser"
|
|
)
|
|
|
|
var settings = make(map[string]string)
|
|
|
|
func main() {
|
|
parseArguments()
|
|
if settings["interactive"] == "false" {
|
|
showQueue()
|
|
}
|
|
if settings["interactive"] == "true" {
|
|
interactiveShell()
|
|
}
|
|
}
|
|
|
|
func parseArguments() {
|
|
mailqCommandPtr := flag.String("mailqCommand", "mailq", "Command to use for getting mailq output")
|
|
mailqCommandArgsPtr := flag.String("mailqCommandArgs", "", "Optional arguments to pass to mailqCommand")
|
|
interactivePtr := flag.Bool("interactive", false, "Set to true for shell mode")
|
|
flag.Parse()
|
|
settings["mailqCommand"] = *mailqCommandPtr
|
|
settings["mailqCommandArgs"] = *mailqCommandArgsPtr
|
|
settings["interactive"] = strconv.FormatBool(*interactivePtr)
|
|
}
|
|
|
|
func interactiveShell() {
|
|
|
|
}
|
|
|
|
func showQueue() {
|
|
var entries []parser.QEntry
|
|
var err error
|
|
if settings["mailqCommandArgs"] == "" {
|
|
entries, err = readFromCmd(exec.Command(settings["mailqCommand"]))
|
|
} else {
|
|
entries, err = readFromCmd(exec.Command(settings["mailqCommand"], settings["mailqCommandArgs"]))
|
|
}
|
|
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 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
|
|
}
|