bocadillo/cmd/main.go

60 lines
1.3 KiB
Go
Raw Normal View History

2018-07-29 19:20:30 +02:00
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
2018-11-05 18:15:59 +01:00
"github.com/localhots/blt/parser"
2018-07-29 19:20:30 +02:00
"github.com/localhots/gobelt/log"
)
func main() {
dsn := flag.String("dsn", "", "Database source name")
id := flag.Uint("id", 1000, "Server ID (arbitrary, unique)")
file := flag.String("file", "", "Binary log file name")
offset := flag.Uint("offset", 0, "Log offset in bytes")
flag.Parse()
ctx := context.Background()
validate((*dsn != ""), "Database source name is not set")
validate((*id != 0), "Server ID is not set")
validate((*file != ""), "Binary log file is not set")
2018-11-05 18:15:59 +01:00
conf := parser.Config{
2018-07-29 19:20:30 +02:00
ServerID: uint32(*id),
File: *file,
Offset: uint32(*offset),
}
2018-11-05 18:15:59 +01:00
reader, err := parser.Connect(*dsn, conf)
2018-07-29 19:20:30 +02:00
if err != nil {
log.Fatalf(ctx, "Failed to establish connection: %v", err)
}
off := conf.Offset
for i := 0; i < 100; i++ {
// for {
evt, err := reader.ReadEventHeader(ctx)
if err != nil {
log.Fatalf(ctx, "Failed to read event: %v", err)
}
ts := time.Unix(int64(evt.Timestamp), 0).Format(time.RFC3339)
log.Info(ctx, "Event received", log.F{
"type": evt.Type,
"timestamp": ts,
"offset": off,
})
off = evt.NextOffset
}
}
func validate(cond bool, msg string) {
if !cond {
fmt.Println(msg)
flag.Usage()
os.Exit(2)
}
}