Properly use methods to build up MailQ struct
This commit is contained in:
parent
fac4985ab7
commit
50edc8dc84
|
@ -19,12 +19,12 @@ func ParseFromReader(dataSource io.Reader) (MailQ, error) {
|
||||||
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 MailQ{}, nil
|
return NewQueue(), nil
|
||||||
} 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 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
|
var currentMail QEntry
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
// Read input line by line
|
// Read input line by line
|
||||||
|
@ -36,16 +36,12 @@ func ParseFromReader(dataSource io.Reader) (MailQ, error) {
|
||||||
} 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]
|
||||||
queue.NumTotal += 1
|
|
||||||
if strings.HasSuffix(messageId, "*") {
|
if strings.HasSuffix(messageId, "*") {
|
||||||
queue.NumActive += 1
|
|
||||||
currentMail.Status = "active"
|
currentMail.Status = "active"
|
||||||
} else if strings.HasSuffix(messageId, "!") {
|
} else if strings.HasSuffix(messageId, "!") {
|
||||||
currentMail.Status = "hold"
|
currentMail.Status = "hold"
|
||||||
queue.NumHold += 1
|
|
||||||
} else {
|
} else {
|
||||||
currentMail.Status = "deferred"
|
currentMail.Status = "deferred"
|
||||||
queue.NumDeferred += 1
|
|
||||||
}
|
}
|
||||||
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)
|
||||||
|
@ -65,13 +61,13 @@ func ParseFromReader(dataSource io.Reader) (MailQ, error) {
|
||||||
} 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
|
||||||
queue.Entries = append(queue.Entries, currentMail)
|
queue.AddEntry(currentMail)
|
||||||
currentMail = QEntry{}
|
currentMail = QEntry{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 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
|
return queue, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ func NewQueue() MailQ {
|
||||||
return MailQ{}
|
return MailQ{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q MailQ) AddEntry(entry QEntry) {
|
func (q *MailQ) AddEntry(entry QEntry) {
|
||||||
q.NumTotal += 1
|
q.NumTotal += 1
|
||||||
if entry.Status == "active" {
|
if entry.Status == "active" {
|
||||||
q.NumActive += 1
|
q.NumActive += 1
|
||||||
|
@ -40,7 +40,7 @@ func (q MailQ) AddEntry(entry QEntry) {
|
||||||
q.Entries = append(q.Entries, entry)
|
q.Entries = append(q.Entries, entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q MailQ) RemoveEntry(entry QEntry) {
|
func (q *MailQ) RemoveEntry(entry QEntry) {
|
||||||
var currentIndex int
|
var currentIndex int
|
||||||
var currentEntry QEntry
|
var currentEntry QEntry
|
||||||
for currentIndex, currentEntry = range q.Entries {
|
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:]...)
|
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 {
|
for _, entry := range q.Entries {
|
||||||
if entry.Id == id {
|
if entry.Id == id {
|
||||||
return entry, nil
|
return entry, nil
|
||||||
|
|
Loading…
Reference in New Issue