Skip to content

Commit

Permalink
Added to Git
Browse files Browse the repository at this point in the history
  • Loading branch information
Per Abich committed Apr 16, 2020
0 parents commit 8fb2dc0
Show file tree
Hide file tree
Showing 8 changed files with 2,478 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
LICENSE
*.iml
/postqueue2json
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters:
enable:
- goimports
55 changes: 55 additions & 0 deletions filter/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package filter

import (
"regexp"
"strings"
)

type QueueItem struct {
QueueId string
Sender string
Queue string
Recipient string
Message string
}

var firstLineExpr = regexp.MustCompile(`^(\w+)([*!])?\s+(\d+)\s+(\w+ \w+ \d+ \d+:\d+:\d+)\s+(\S+@\S+)$`)
var msgLineExpr = regexp.MustCompile(`^\s*\((.*)\)$`)
var recipientExpr = regexp.MustCompile(`^\s*(\S+@\S+)$`)

func ConvertPostqueueToQueueItem(lines <-chan string) (<-chan QueueItem, error) {
items := make(chan QueueItem)
go func() {
defer close(items)
var item QueueItem
for line := range lines {
if strings.HasPrefix(line, "-") {
continue
}
switch {
case strings.TrimSpace(line) == "":
if item.QueueId != "" {
items <- item
}
item = QueueItem{}
case firstLineExpr.MatchString(line):
firstLine := firstLineExpr.FindStringSubmatch(line)
item.QueueId = firstLine[1]
switch firstLine[2] {
case "!":
item.Queue = "hold"
case "*":
item.Queue = "active"
default:
item.Queue = "other"
}
item.Sender = firstLine[5]
case msgLineExpr.MatchString(line):
item.Message = msgLineExpr.FindStringSubmatch(line)[1]
case recipientExpr.MatchString(line):
item.Recipient = recipientExpr.FindStringSubmatch(line)[1]
}
}
}()
return items, nil
}
73 changes: 73 additions & 0 deletions filter/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package filter

import (
"bufio"
"os"
"strings"
"testing"
)

func TestConvertPostqueueToQueueItem(t *testing.T) {
type args struct {
filename string
}
type wants struct {
count int
}
tests := []struct {
name string
args args
want wants
wantErr bool
}{
{
name: "basic test",
args: args{
filename: "testdata/postqueue.txt",
},
want: wants{count: 654},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lines := make(chan string)
go func() {
defer close(lines)
file, err := os.Open(tt.args.filename)
if err != nil {
t.Errorf("failed to open file %v: %v", tt.args.filename, err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines <- scanner.Text()
}
}()

queueItems, err := ConvertPostqueueToQueueItem(lines)
if (err != nil) != tt.wantErr {
t.Errorf("ConvertPostqueueToQueueItem() error = %v, wantErr %v", err, tt.wantErr)
return
}
counter := 0
for queueItem := range queueItems {
counter++
if queueItem.QueueId == "" {
t.Errorf("No QueueId found for item %v", queueItem)
}
if strings.HasSuffix(queueItem.QueueId, "*") {
t.Errorf("Not split QueueID correct: %v", queueItem.QueueId)
}
if queueItem.Sender == "" {
t.Errorf("No sender found for item %v", queueItem)
}
if queueItem.Recipient == "" {
t.Errorf("No recipient found for item %v", queueItem)
}
}
if counter != tt.want.count {
t.Errorf("Expected %v items but queueItems %v", tt.want.count, counter)
}
})
}
}
Loading

0 comments on commit 8fb2dc0

Please sign in to comment.