GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — binlog-parser/main.go at master · oprstchn/binlog-parser

Expand file tree

/

main.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
118 lines (88 loc) · 2.76 KB
/

main.go

Copy path

File metadata and controls

118 lines (88 loc) · 2.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"flag"
"fmt"
"github.com/golang/glog"
"os"
"path"
"strings"
"github.com/oprstchn/binlog-parser/parser"
"github.com/oprstchn/binlog-parser/file_parser"
_ "github.com/joho/godotenv/autoload"
)
var prettyPrintJsonFlag = flag.Bool("prettyprint", false, "Pretty print json")
var includeTablesFlag = flag.String("include_tables", "", "comma-separated list of tables to include")
var includeSchemasFlag = flag.String("include_schemas", "", "comma-separated list of schemas to include")
var outDirFlag = flag.String("out_dir", "", "If non-empty, write log files in this directory")
func main() {
flag.Usage = func() {
printUsage()
}
flag.Parse()
if flag.NArg() != 1 {
printUsage()
os.Exit(1)
}
binlogFilename := flag.Arg(0)
dbDsn := os.Getenv("DB_DSN")
if dbDsn == "" {
fmt.Fprint(os.Stderr, "Please set env variable DB_DSN to a valid MySQL connection string")
os.Exit(1)
}
glog.V(1).Infof("Will parse file %s", binlogFilename)
parseFunc := file_parser.CreateBinlogParseFunc(dbDsn, consumerChainFromArgs())
err := parseFunc(binlogFilename)
if err != nil {
fmt.Fprintf(os.Stderr, "Got error: %s\n", err)
os.Exit(1)
}
}
func consumerChainFromArgs() parser.ConsumerChain {
chain := parser.NewConsumerChain()
stream := os.Stdout
defer stream.Close()
if *outDirFlag != "" {
chain.OutDir(*outDirFlag)
file, err := os.Create(*outDirFlag)
if err != nil {
panic(err)
}
stream = file
}
//chain.CollectAsJson(os.Stdout, *prettyPrintJsonFlag)
chain.CollectAsJson(stream, *prettyPrintJsonFlag)
glog.V(1).Infof("Pretty print JSON %s", *prettyPrintJsonFlag)
if *includeTablesFlag != "" {
includeTables := commaSeparatedListToArray(*includeTablesFlag)
chain.IncludeTables(includeTables...)
glog.V(1).Infof("Including tables %v", includeTables)
}
if *includeSchemasFlag != "" {
includeSchemas := commaSeparatedListToArray(*includeSchemasFlag)
chain.IncludeSchemas(includeSchemas...)
glog.V(1).Infof("Including schemas %v", includeSchemas)
}
return chain
}
func printUsage() {
binName := path.Base(os.Args[0])
usage := "Parse a binlog file, dump JSON to stdout. Includes options to filter by schema and table.\n" +
"Reads from information_schema database to find out the field names for a row event.\n\n" +
"Usage:\t%s [options ...] binlog\n\n" +
"Options are:\n\n"
fmt.Fprintf(os.Stderr, usage, binName)
flag.PrintDefaults()
envVars := "\nRequired environment variables:\n\n" +
"DB_DSN\t Database connection string, needs read access to information_schema\n"
fmt.Fprint(os.Stderr, envVars)
}
func commaSeparatedListToArray(str string) []string {
var arr []string
for _, item := range strings.Split(str, ",") {
item = strings.TrimSpace(item)
if item != "" {
arr = append(arr, item)
}
}
return arr
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.