-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Per Abich
committed
Apr 16, 2020
0 parents
commit 8fb2dc0
Showing
8 changed files
with
2,478 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea/ | ||
LICENSE | ||
*.iml | ||
/postqueue2json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
linters: | ||
enable: | ||
- goimports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.