forked from MemeLabs/overrustlelogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
55 lines (46 loc) · 1.33 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package common
import (
"fmt"
"regexp"
"strings"
"time"
)
// Log timestamp format
var (
MessageTimeLayout = "[2006-01-02 15:04:05 MST] "
MessageTimeLayoutLength = len(MessageTimeLayout)
MessageDateLayout = "2006-01-02"
)
// ParseMessageLine parse log line into message struct
func ParseMessageLine(b string) (*Message, error) {
if len(b) < MessageTimeLayoutLength {
return nil, fmt.Errorf("supplied line is too short to be parsed as message: %s", b)
}
ts, err := time.Parse(MessageTimeLayout, b[:MessageTimeLayoutLength])
if err != nil {
return nil, fmt.Errorf("malformed date in message: %v", err)
}
b = b[MessageTimeLayoutLength:]
nickLength := strings.IndexRune(b, ':')
if nickLength >= len(b) || nickLength == -1 {
return nil, fmt.Errorf("malformed nick in message: %s", b)
}
// should never happen
if nickLength+2 > len(b) {
return nil, fmt.Errorf("nickLength is longer than whole line")
}
return &Message{
Nick: b[:nickLength],
Data: b[nickLength+2:],
Time: ts,
}, nil
}
var channelPathPattern = regexp.MustCompile("/([a-zA-Z0-9_]+) chatlog/")
// ExtractChannelFromPath ...
func ExtractChannelFromPath(p string) (string, error) {
match := channelPathPattern.FindStringSubmatch(p)
if match == nil {
return "", fmt.Errorf("supplied path does not contain channel name: %s", p)
}
return match[1], nil
}