From d087d8d746537725a6bce674527b09147167e96f Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 30 Dec 2018 12:54:22 +0100 Subject: [PATCH] Fix reading from command not passing arguments --- main.go | 16 +--------------- parser/parser.go | 7 ++++--- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 833a3af..dafae4a 100644 --- a/main.go +++ b/main.go @@ -25,12 +25,10 @@ func main() { } func parseArguments() { - inputPtr := flag.String("input", "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") interactivePtr := flag.Bool("interactive", false, "Set to true for shell mode") flag.Parse() - settings["input"] = *inputPtr settings["mailqCommand"] = *mailqCommandPtr settings["mailqCommandArgs"] = *mailqCommandArgsPtr settings["interactive"] = strconv.FormatBool(*interactivePtr) @@ -43,15 +41,7 @@ func interactiveShell() { func showQueue() { var entries []parser.QEntry var err error - if settings["input"] == "stdin" { - entries, err = readFromStdin() - } else if settings["input"] == "cmd" { - entries, err = readFromCmd(exec.Command(settings["mailqCommand"])) - } else { - fmt.Printf("Unsupported input!") - flag.PrintDefaults() - os.Exit(1) - } + entries, err = readFromCmd(exec.Command(settings["mailqCommand"], settings["mailqCommandArgs"])) if err != nil { fmt.Printf("Error reading: %s\n", err.Error()) os.Exit(1) @@ -61,10 +51,6 @@ func showQueue() { } } -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 { diff --git a/parser/parser.go b/parser/parser.go index 4964a78..1a37183 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -39,9 +39,10 @@ func ParseMailQ(dataSource io.Reader) ([]QEntry, error) { const dateFormat = "2006 Mon Jan _2 15:04:05" var messageIdStart = regexp.MustCompile("^[0-9A-Za-z]+[*!]? ") scanner := bufio.NewScanner(dataSource) - scanner.Scan() - line := scanner.Text() - if strings.HasPrefix(line, "Mail queue is empty") { + var line string + scanner.Scan() + line = scanner.Text() + if strings.HasPrefix(line, "Mail queue is empty") { // If mail queue is empty, there is nothing to do return nil, errors.New(line) } else if strings.HasPrefix(line, "-Queue ID-") == false {