-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
190 lines (168 loc) · 5.34 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/Masterminds/sprig"
"github.com/davecgh/go-spew/spew"
"github.com/jessevdk/go-flags"
"github.com/pkg/errors"
"github.com/reddec/fluent-amqp"
"github.com/streadway/amqp"
"io"
"io/ioutil"
"log"
"os"
"text/template"
"time"
)
var (
version = "dev"
)
var config struct {
URLs []string `short:"u" long:"url" env:"BROKER_URL" description:"One or more AMQP brokers urls" default:"amqp://guest:guest@localhost" required:"yes"`
Exchange string `short:"e" long:"exchange" env:"BROKER_EXCHANGE" description:"Name of AMQP exchange. Can be empty"`
ExchangeType string `short:"k" long:"kind" env:"BROKER_KIND" description:"Exchange kind" choice:"direct" choice:"topic" choice:"fanout" default:"direct"`
Verify string `short:"s" long:"verify-public-cert" env:"BROKER_SIGN" description:"Path to public cert to verify"`
Queue string `short:"Q" long:"queue" env:"BROKER_QUEUE" description:"Queue name or empty for autogenerated"`
Lazy bool `short:"l" long:"lazy" env:"BROKER_LAZY" description:"Make queue lazy (prefer keep data on disk)"`
OutType string `short:"o" long:"output" env:"BROKER_OUTPUT" description:"Output type" choice:"body" choice:"dump" choice:"json" choice:"template" default:"body"`
Args struct {
RoutingKey string `positional-arg-name:"routing-key" env:"BROKER_ROUTING_KEY" description:"Routing key"`
} `positional-args:"yes"`
Interval time.Duration `short:"R" long:"reconnect-interval" env:"BROKER_RECONNECT_INTERVAL" description:"Reconnect timeout" default:"5s"`
Timeout time.Duration `short:"T" long:"timeout" env:"BROKER_CONNECT_TIMEOUT" description:"Connect timeout" default:"30s"`
Quiet bool `short:"q" long:"quiet" env:"BROKER_QUIET" description:"Suppress all log messages"`
Version bool `short:"v" long:"version" description:"Print version and exit"`
}
var logOutput io.Writer = os.Stderr
type dumpHandler struct{}
func (dh *dumpHandler) Handle(ctx context.Context, msg amqp.Delivery) {
spew.Dump(msg)
}
type jsonHandler struct{}
func (dh *jsonHandler) Handle(ctx context.Context, msg amqp.Delivery) {
dec := json.NewEncoder(os.Stdout)
dec.SetIndent("", " ")
err := dec.Encode(&msg)
if err != nil {
panic(err)
}
}
type plainHandler struct{}
func (dh *plainHandler) Handle(ctx context.Context, msg amqp.Delivery) {
_, err := os.Stdout.Write(msg.Body)
if err != nil {
panic(err)
}
}
type templateHandler struct {
t *template.Template
}
func (dh *templateHandler) Handle(ctx context.Context, msg amqp.Delivery) {
err := dh.t.Execute(os.Stdout, msg)
if err != nil {
panic(err)
}
}
type limited struct {
Num int
HandlerFunc fluent.SinkHandlerFunc
idx int
}
func (dh *limited) Handle(ctx context.Context, msg amqp.Delivery) error {
if dh.idx >= dh.Num {
return errors.New("limit reached")
}
dh.idx++
dh.HandlerFunc(ctx, msg)
return nil
}
func run() error {
gctx, cancel := context.WithCancel(context.Background())
ctx := fluent.SignalContext(gctx)
broker := fluent.Broker(config.URLs...).Context(ctx).Logger(log.New(logOutput, "[broker] ", log.LstdFlags)).Interval(config.Interval).Timeout(config.Timeout).Start()
defer broker.WaitToFinish()
defer cancel()
log.Println("preparing sink")
publisherCfg := broker.Sink(config.Queue)
if config.Verify != "" {
log.Println("preparing validator")
publisherCfg = publisherCfg.Validate(config.Verify)
}
if config.Lazy {
publisherCfg = publisherCfg.Lazy()
}
var handler fluent.SimpleHandler
switch config.OutType {
case "dump":
handler = &dumpHandler{}
case "json":
handler = &jsonHandler{}
case "body", "plain":
handler = &plainHandler{}
case "template":
log.Println("reading template from STDIN")
data, err := ioutil.ReadAll(os.Stdin)
if err != nil && err != io.EOF {
return err
}
funcs := sprig.TxtFuncMap()
funcs["asText"] = func(data []byte) string { return string(data) }
t, err := template.New("").Funcs(funcs).Parse(string(data))
if err != nil {
return err
}
handler = &templateHandler{t}
default:
panic("unknown output format")
}
handlerFunc := func(ctx context.Context, msg amqp.Delivery) {
handler.Handle(ctx, msg)
cancel()
}
var exc *fluent.Exchange
if config.Exchange != "" {
switch config.ExchangeType {
case "topic":
exc = publisherCfg.Topic(config.Exchange)
case "direct":
exc = publisherCfg.Direct(config.Exchange)
case "fanout":
exc = publisherCfg.Fanout(config.Exchange)
default:
return errors.Errorf("unknown exchange type %v", config.ExchangeType)
}
if config.Args.RoutingKey != "" {
exc = exc.Key(config.Args.RoutingKey)
}
exc.Transact(&limited{Num: 1, HandlerFunc: handlerFunc})
} else {
publisherCfg.Transact(&limited{Num: 1, HandlerFunc: handlerFunc})
}
log.Println("reader prepared")
log.Println("waiting for messages...")
broker.WaitToFinish()
return nil
}
func main() {
parser := flags.NewParser(&config, flags.Default)
_, err := parser.Parse()
if config.Version {
fmt.Println(version)
return
}
if err != nil {
os.Exit(1)
}
if config.Quiet {
logOutput = ioutil.Discard
}
log.SetPrefix("[recv ] ")
log.SetOutput(logOutput)
err = run()
if err != nil {
log.Println("failed:", err)
os.Exit(2)
}
}