Add support for JSON output
This commit is contained in:
parent
92a709d196
commit
3d893015a3
12
main.go
12
main.go
@ -29,13 +29,13 @@ func parseArguments() {
|
|||||||
mailqCommandPtr := flag.String("mailqCommand", "mailq", "Command to use for getting mailq output")
|
mailqCommandPtr := flag.String("mailqCommand", "mailq", "Command to use for getting mailq output")
|
||||||
mailqCommandArgsPtr := flag.String("mailqCommandArgs", "", "Optional arguments to pass to mailqCommand")
|
mailqCommandArgsPtr := flag.String("mailqCommandArgs", "", "Optional arguments to pass to mailqCommand")
|
||||||
interactivePtr := flag.Bool("interactive", false, "Set to true for shell mode")
|
interactivePtr := flag.Bool("interactive", false, "Set to true for shell mode")
|
||||||
machineFormatPtr := flag.Bool("machineFormat", false, "Set to true to get output suitable for cut/awk/...")
|
outputFormatPtr := flag.String("outputFormat", "human", "Available output formats: \"human\", \"machine\", \"json\"")
|
||||||
usePagerPtr := flag.Bool("usePager", true, "Use pager to display output?")
|
usePagerPtr := flag.Bool("usePager", true, "Use pager to display output?")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
settings["mailqCommand"] = *mailqCommandPtr
|
settings["mailqCommand"] = *mailqCommandPtr
|
||||||
settings["mailqCommandArgs"] = *mailqCommandArgsPtr
|
settings["mailqCommandArgs"] = *mailqCommandArgsPtr
|
||||||
settings["interactive"] = strconv.FormatBool(*interactivePtr)
|
settings["interactive"] = strconv.FormatBool(*interactivePtr)
|
||||||
settings["machineFormat"] = strconv.FormatBool(*machineFormatPtr)
|
settings["outputFormat"] = *outputFormatPtr
|
||||||
settings["usePager"] = strconv.FormatBool(*usePagerPtr)
|
settings["usePager"] = strconv.FormatBool(*usePagerPtr)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,10 +63,12 @@ func showQueue() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func printQueue(queue parser.MailQ, writer io.Writer) {
|
func printQueue(queue parser.MailQ, writer io.Writer) {
|
||||||
if settings["machineFormat"] == "true" {
|
if settings["outputFormat"] == "human" {
|
||||||
queue.PrintMachineReadable(writer)
|
|
||||||
} else {
|
|
||||||
queue.PrintHumanReadable(writer)
|
queue.PrintHumanReadable(writer)
|
||||||
|
} else if settings["outputFormat"] == "machine" {
|
||||||
|
queue.PrintMachineReadable(writer)
|
||||||
|
} else if settings["outputFormat"] == "json" {
|
||||||
|
queue.PrintJSON(writer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package parser
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -76,6 +77,15 @@ func (queue MailQ) PrintHumanReadable(writer io.Writer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (queue MailQ) PrintJSON(writer io.Writer) {
|
||||||
|
bytes, err := json.Marshal(queue)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(writer, "Error encoding queue to JSON: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(writer, "%s", bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ParseMailQ(dataSource io.Reader) (MailQ, error) {
|
func ParseMailQ(dataSource io.Reader) (MailQ, error) {
|
||||||
const dateFormat = "2006 Mon Jan _2 15:04:05"
|
const dateFormat = "2006 Mon Jan _2 15:04:05"
|
||||||
var messageIdStart = regexp.MustCompile("^[0-9A-Za-z]+[*!]? ")
|
var messageIdStart = regexp.MustCompile("^[0-9A-Za-z]+[*!]? ")
|
||||||
|
Loading…
Reference in New Issue
Block a user