Ran go fmt on it
This commit is contained in:
parent
50694f861b
commit
40c3044c84
33
main.go
33
main.go
|
@ -1,24 +1,23 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"mailq-inspector/parser"
|
||||||
"mailq-inspector/parser"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Read data from stdin
|
// Read data from stdin
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
entries, err := parser.ParseMailQ(scanner)
|
entries, err := parser.ParseMailQ(scanner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Got error from parser: %s\n", err.Error())
|
fmt.Printf("Got error from parser: %s\n", err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else {
|
} else {
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
fmt.Printf("%s\n", entry.String())
|
fmt.Printf("%s\n", entry.String())
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
144
parser/parser.go
144
parser/parser.go
|
@ -1,86 +1,86 @@
|
||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Entry struct {
|
type Entry struct {
|
||||||
id string
|
id string
|
||||||
status string
|
status string
|
||||||
date time.Time
|
date time.Time
|
||||||
size int
|
size int
|
||||||
sender string
|
sender string
|
||||||
recipients []string
|
recipients []string
|
||||||
reason string
|
reason string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Entry) String() string {
|
func (m Entry) String() string {
|
||||||
return fmt.Sprintf("ID: %s, Status: %s, Size: %d bytes, Sender: %s, Recipients: %d", m.id, m.status, m.size, m.sender, len(m.recipients))
|
return fmt.Sprintf("ID: %s, Status: %s, Size: %d bytes, Sender: %s, Recipients: %d", m.id, m.status, m.size, m.sender, len(m.recipients))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseMailQ(scanner *bufio.Scanner) ([]Entry, error) {
|
func ParseMailQ(scanner *bufio.Scanner) ([]Entry, 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]+[*!]? ")
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
if strings.HasPrefix(line, "Mail queue is empty") {
|
if strings.HasPrefix(line, "Mail queue is empty") {
|
||||||
// If mail queue is empty, there is nothing to do
|
// If mail queue is empty, there is nothing to do
|
||||||
return nil, errors.New(line)
|
return nil, errors.New(line)
|
||||||
} else if strings.HasPrefix(line, "-Queue ID-") == false {
|
} else if strings.HasPrefix(line, "-Queue ID-") == false {
|
||||||
// Abort if input does not look like output from mailq(1)
|
// Abort if input does not look like output from mailq(1)
|
||||||
return nil, errors.New("Sorry, this does not look like output from mailq(1).")
|
return nil, errors.New("Sorry, this does not look like output from mailq(1).")
|
||||||
}
|
}
|
||||||
var currentMail Entry
|
var currentMail Entry
|
||||||
var queueEntries []Entry
|
var queueEntries []Entry
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
// Read input line by line
|
// Read input line by line
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
fields := strings.Fields(line)
|
fields := strings.Fields(line)
|
||||||
if strings.HasPrefix(line, "--") {
|
if strings.HasPrefix(line, "--") {
|
||||||
// Handle the summary line at the end of mailq output
|
// Handle the summary line at the end of mailq output
|
||||||
break
|
break
|
||||||
} else if messageIdStart.MatchString(line) {
|
} else if messageIdStart.MatchString(line) {
|
||||||
// Handle line starting next mail in queue
|
// Handle line starting next mail in queue
|
||||||
messageId := fields[0]
|
messageId := fields[0]
|
||||||
if strings.HasSuffix(messageId, "*") {
|
if strings.HasSuffix(messageId, "*") {
|
||||||
currentMail.status = "active"
|
currentMail.status = "active"
|
||||||
} else if strings.HasSuffix(messageId, "!") {
|
} else if strings.HasSuffix(messageId, "!") {
|
||||||
currentMail.status = "hold"
|
currentMail.status = "hold"
|
||||||
} else {
|
} else {
|
||||||
currentMail.status = "deferred"
|
currentMail.status = "deferred"
|
||||||
}
|
}
|
||||||
dateString := strconv.Itoa(time.Now().Year()) + " " + strings.Join(fields[2:6], " ")
|
dateString := strconv.Itoa(time.Now().Year()) + " " + strings.Join(fields[2:6], " ")
|
||||||
mailDate, _ := time.Parse(dateFormat, dateString)
|
mailDate, _ := time.Parse(dateFormat, dateString)
|
||||||
currentMail.id = strings.TrimRight(messageId, "*!")
|
currentMail.id = strings.TrimRight(messageId, "*!")
|
||||||
currentMail.size, _ = strconv.Atoi(fields[1])
|
currentMail.size, _ = strconv.Atoi(fields[1])
|
||||||
currentMail.date = mailDate
|
currentMail.date = mailDate
|
||||||
currentMail.sender = fields[6]
|
currentMail.sender = fields[6]
|
||||||
continue
|
continue
|
||||||
} else if strings.HasPrefix(line, "(") && strings.HasSuffix(line, ")") {
|
} else if strings.HasPrefix(line, "(") && strings.HasSuffix(line, ")") {
|
||||||
// Handle reason for deferred status (if deferred at all, may be missing)
|
// Handle reason for deferred status (if deferred at all, may be missing)
|
||||||
currentMail.reason = strings.Trim(strings.TrimSpace(line), "()")
|
currentMail.reason = strings.Trim(strings.TrimSpace(line), "()")
|
||||||
continue
|
continue
|
||||||
} else if len(fields) == 1 {
|
} else if len(fields) == 1 {
|
||||||
// Handle line with one of the mail recipients
|
// Handle line with one of the mail recipients
|
||||||
currentMail.recipients = append(currentMail.recipients, fields[0])
|
currentMail.recipients = append(currentMail.recipients, fields[0])
|
||||||
continue
|
continue
|
||||||
} else if len(fields) == 0 {
|
} else if len(fields) == 0 {
|
||||||
// If the next line is empty, make sure to push current mail to list
|
// If the next line is empty, make sure to push current mail to list
|
||||||
// and create a new struct for the next mail to process
|
// and create a new struct for the next mail to process
|
||||||
queueEntries = append(queueEntries, currentMail)
|
queueEntries = append(queueEntries, currentMail)
|
||||||
currentMail = Entry{}
|
currentMail = Entry{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if scanner.Err() != nil {
|
if scanner.Err() != nil {
|
||||||
// If the scanner failed, let our caller know.
|
// If the scanner failed, let our caller know.
|
||||||
return nil, errors.New("Something went wrong with reading from stdin. Sorry!")
|
return nil, errors.New("Something went wrong with reading from stdin. Sorry!")
|
||||||
}
|
}
|
||||||
return queueEntries, nil
|
return queueEntries, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue