-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafkapub.go
106 lines (104 loc) · 2.48 KB
/
kafkapub.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
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
package main
import (
"github.com/godaner/brokerc/broker"
"github.com/godaner/brokerc/broker/kafkav1"
"github.com/urfave/cli"
"strings"
)
var KafkaPublishCommand = cli.Command{
Name: "kafkapub",
Usage: "publish kafka message",
UsageText: "Usage: brokerc kafkapub [options...] <uri>, uri arg format: host.domain[:port],host.domain[:port],...",
Flags: []cli.Flag{
cli.StringFlag{
Name: "t",
Usage: "topic , this topic will be created when both parameters p and r are not 0.",
Required: true,
},
cli.StringFlag{
Name: "m",
Usage: "message.",
Required: true,
},
cli.IntFlag{
Name: "p",
Usage: "part number.",
Required: false,
},
cli.IntFlag{
Name: "r",
Usage: "replica number.",
Required: false,
},
cli.BoolFlag{
Name: "d",
Usage: "debug.",
Required: false,
},
cli.StringFlag{
Name: "cafile",
Usage: "path to a file containing trusted CA certificates to enable encrypted communication.",
Required: false,
},
cli.StringFlag{
Name: "cert",
Usage: "client certificate for authentication, if required by server.",
Required: false,
},
cli.StringFlag{
Name: "key",
Usage: "client private key for authentication, if required by server.",
Required: false,
},
cli.BoolFlag{
Name: "insecure",
Usage: "do not check that the server certificate hostname matches the remote hostname.",
Required: false,
},
},
Action: func(context *cli.Context) error {
uri := context.Args().Get(0)
m, t, d, cafile, cert, key, insecure, p, r :=
context.String("m"),
context.String("t"),
context.Bool("d"),
context.String("cafile"),
context.String("cert"),
context.String("key"),
context.Bool("insecure"),
context.Int("p"),
context.Int("r")
logger.SetDebug(d)
var kt *kafkav1.TLS
if insecure || cafile != "" || cert != "" || key != "" {
kt = &kafkav1.TLS{
Insecure: insecure,
CertFile: cafile,
KeyFile: cert,
CaCertFile: key,
}
}
b := kafkav1.KafkaBrokerV1{
Logger: logger,
URIs: strings.Split(uri, ","),
TLS: kt,
Insecure: insecure,
SubSarama: nil,
}
err := b.Connect()
if err != nil {
return err
}
defer b.Disconnect()
err = b.Publish(t, &broker.Message{
Header: nil,
Body: []byte(m),
}, broker.SetPubPart(p),
broker.SetPubReplica(r))
if err != nil {
return err
}
logger.Infof("PUBLISH=> uri:%v, t:%v, m:%v !", uri, t, m)
return nil
},
}