Properly use methods to build up MailQ struct

This commit is contained in:
Jan Philipp Timme 2019-01-06 19:07:10 +01:00
parent fac4985ab7
commit 50edc8dc84
2 changed files with 8 additions and 12 deletions

View File

@ -19,12 +19,12 @@ func ParseFromReader(dataSource io.Reader) (MailQ, error) {
line = scanner.Text()
if strings.HasPrefix(line, "Mail queue is empty") {
// If mail queue is empty, there is nothing to do
return MailQ{}, nil
return NewQueue(), nil
} else if strings.HasPrefix(line, "-Queue ID-") == false {
// Abort if input does not look like output from mailq(1)
return MailQ{}, errors.New("Sorry, this does not look like output from mailq(1).")
return NewQueue(), errors.New("Sorry, this does not look like output from mailq(1).")
}
var queue MailQ
queue := NewQueue()
var currentMail QEntry
for scanner.Scan() {
// Read input line by line
@ -36,16 +36,12 @@ func ParseFromReader(dataSource io.Reader) (MailQ, error) {
} else if messageIdStart.MatchString(line) {
// Handle line starting next mail in queue
messageId := fields[0]
queue.NumTotal += 1
if strings.HasSuffix(messageId, "*") {
queue.NumActive += 1
currentMail.Status = "active"
} else if strings.HasSuffix(messageId, "!") {
currentMail.Status = "hold"
queue.NumHold += 1
} else {
currentMail.Status = "deferred"
queue.NumDeferred += 1
}
dateString := strconv.Itoa(time.Now().Year()) + " " + strings.Join(fields[2:6], " ")
mailDate, _ := time.Parse(dateFormat, dateString)
@ -65,13 +61,13 @@ func ParseFromReader(dataSource io.Reader) (MailQ, error) {
} else if len(fields) == 0 {
// 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
queue.Entries = append(queue.Entries, currentMail)
queue.AddEntry(currentMail)
currentMail = QEntry{}
}
}
if scanner.Err() != nil {
// If the scanner failed, let our caller know.
return MailQ{}, errors.New("Something went wrong with reading from stdin. Sorry!")
return NewQueue(), errors.New("Something went wrong with reading from stdin. Sorry!")
}
return queue, nil
}

View File

@ -28,7 +28,7 @@ func NewQueue() MailQ {
return MailQ{}
}
func (q MailQ) AddEntry(entry QEntry) {
func (q *MailQ) AddEntry(entry QEntry) {
q.NumTotal += 1
if entry.Status == "active" {
q.NumActive += 1
@ -40,7 +40,7 @@ func (q MailQ) AddEntry(entry QEntry) {
q.Entries = append(q.Entries, entry)
}
func (q MailQ) RemoveEntry(entry QEntry) {
func (q *MailQ) RemoveEntry(entry QEntry) {
var currentIndex int
var currentEntry QEntry
for currentIndex, currentEntry = range q.Entries {
@ -51,7 +51,7 @@ func (q MailQ) RemoveEntry(entry QEntry) {
q.Entries = append(q.Entries[:currentIndex], q.Entries[currentIndex+1:]...)
}
func (q MailQ) GetMessageById(id string) (QEntry, error) {
func (q *MailQ) GetMessageById(id string) (QEntry, error) {
for _, entry := range q.Entries {
if entry.Id == id {
return entry, nil