Files
learngo/logparser/v5/pipe/parse/text.go

45 lines
760 B
Go
Raw Normal View History

2019-08-17 15:55:25 +03:00
// For more tutorials: https://bp.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
2019-08-28 18:54:57 +03:00
package parse
2019-08-17 15:55:25 +03:00
import (
"bufio"
"io"
2019-08-28 18:54:57 +03:00
2019-08-28 20:23:38 +03:00
"github.com/inancgumus/learngo/logparser/v5/pipe"
2019-08-17 15:55:25 +03:00
)
2019-08-28 18:54:57 +03:00
// Text parses text based log lines.
type Text struct {
2019-08-26 14:37:58 +03:00
reader io.Reader
2019-08-17 15:55:25 +03:00
}
2019-08-28 18:54:57 +03:00
// FromText creates a text parser.
func FromText(r io.Reader) *Text {
return &Text{reader: r}
2019-08-17 15:55:25 +03:00
}
2019-08-28 18:54:57 +03:00
// Each yields records from a text log.
func (p *Text) Each(yield func(pipe.Record)) error {
2019-08-26 14:37:58 +03:00
defer readClose(p.reader)
2019-08-17 15:55:25 +03:00
2019-08-26 21:52:47 +03:00
in := bufio.NewScanner(p.reader)
2019-08-17 15:55:25 +03:00
for in.Scan() {
r := new(record)
2019-08-26 21:52:47 +03:00
if err := r.UnmarshalText(in.Bytes()); err != nil {
return err
2019-08-17 15:55:25 +03:00
}
2019-08-28 18:54:57 +03:00
yield(r)
2019-08-17 15:55:25 +03:00
}
return in.Err()
}