Fix reading from command not passing arguments

This commit is contained in:
Jan Philipp Timme 2018-12-30 12:54:22 +01:00
parent 5474966220
commit d087d8d746
2 changed files with 5 additions and 18 deletions

16
main.go
View File

@ -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 {

View File

@ -39,8 +39,9 @@ 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)
var line string
scanner.Scan()
line := scanner.Text()
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)