Skip to content

Commit c376a7b

Browse files
committed
Implement Sougou suggestions
1 parent e92665b commit c376a7b

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

vreplgen/vreplgen.go

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,37 @@ import (
3232
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
3333
)
3434

35-
var onDdl string
35+
var onDDL string
3636

3737
func init() {
38-
flag.StringVar(&onDdl, "on_ddl", "ignore", "Set on_ddl value for replication stream - ignore, stop, exec, exec_ignore")
39-
flag.Parse()
38+
flag.StringVar(&onDDL, "on_ddl", "ignore", "Set on_ddl value for replication stream - ignore, stop, exec, exec_ignore")
4039
}
4140

4241
func main() {
43-
argOffset := 0
44-
if (len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "-")) {
45-
argOffset = 2
46-
}
42+
flag.Parse()
4743

48-
if (len(os.Args) < (7+argOffset)) {
44+
if len(os.Args) < 9 {
4945
fmt.Println("Usage: vreplgen [-on_ddl (ignore|stop|exec|exec_ignore)] <tablet_id> <src_keyspace> <src_shard> <dest_keyspace> <dest_table1> 'filter1' [<dest_table2> 'filter2']...")
5046
os.Exit(1)
5147
}
5248

5349
vtctl := os.Getenv("VTCTLCLIENT")
54-
if (vtctl == "") {
55-
vtctl = "vtctlclient -server localhost:15999"
50+
if vtctl == "" {
51+
vtctl = "vtctlclient -server localhost:15999"
5652
}
57-
tabletID := os.Args[1+argOffset]
58-
sourceKeyspace := os.Args[2+argOffset]
59-
sourceShard := os.Args[3+argOffset]
60-
destKeyspace := os.Args[4+argOffset]
61-
destDbName := "vt_" + destKeyspace
53+
54+
// First, we process fixed positional arguments
55+
// such as the intended target and source
56+
tabletID := os.Args[3]
57+
sourceKeyspace := os.Args[4]
58+
sourceShard := os.Args[5]
59+
destKeyspace := os.Args[6]
60+
destDbName := destKeyspace
6261
var rules []*binlogdatapb.Rule
63-
for i := 5+argOffset; i < len(os.Args); i = i+2 {
62+
63+
// Next, we iterate over all possible rules
64+
// Note this can be a variable number!
65+
for i := 7; i < len(os.Args); i = i + 2 {
6466
destTable := os.Args[i]
6567
destFilter := os.Args[i+1]
6668
rule := new(binlogdatapb.Rule)
@@ -72,23 +74,13 @@ func main() {
7274
Rules: rules,
7375
}
7476

75-
var onDdlAction binlogdatapb.OnDDLAction
76-
switch onDdl {
77-
case "ignore":
78-
onDdlAction = binlogdatapb.OnDDLAction_IGNORE
79-
case "stop":
80-
onDdlAction = binlogdatapb.OnDDLAction_STOP
81-
case "exec":
82-
onDdlAction = binlogdatapb.OnDDLAction_EXEC
83-
case "exec_ignore":
84-
onDdlAction = binlogdatapb.OnDDLAction_EXEC_IGNORE
85-
}
77+
onDDLAction := binlogdatapb.OnDDLAction(binlogdatapb.OnDDLAction_value[strings.ToUpper(onDDL)])
8678

8779
bls := &binlogdatapb.BinlogSource{
8880
Keyspace: sourceKeyspace,
8981
Shard: sourceShard,
9082
Filter: filter,
91-
OnDdl: onDdlAction,
83+
OnDdl: onDDLAction,
9284
}
9385
val := sqltypes.NewVarBinary(fmt.Sprintf("%v", bls))
9486
var sqlEscaped bytes.Buffer

0 commit comments

Comments
 (0)