-
Notifications
You must be signed in to change notification settings - Fork 16
/
cmd_publish.go
115 lines (101 loc) · 3.5 KB
/
cmd_publish.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
// Copyright (C) 2017 Jan Delgado
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"os"
"github.com/jandelgado/rabtap/pkg"
"github.com/streadway/amqp"
)
// CmdPublishArg contains arguments for the publish command
type CmdPublishArg struct {
amqpURI string
tlsConfig *tls.Config
exchange string
routingKey string
readNextMessageFunc MessageReaderFunc
}
// MessageReaderFunc provides messages that can be sent to an exchange. If no
// more messages are available io.EOF is returned as error.
type MessageReaderFunc func() (amqp.Publishing, error)
// SignalChannel transports os.Signal objects like e.g. os.Interrupt
// type SignalChannel chan os.Signal
// publishMessage publishes a single message on the given exchange with the
// provided routingkey
func publishMessage(publishChannel rabtap.PublishChannel,
exchange, routingKey string,
amqpPublishing amqp.Publishing) {
log.Debugf("publishing message %+v to exchange %s with routing key %s",
amqpPublishing, exchange, routingKey)
publishChannel <- &rabtap.PublishMessage{
Exchange: exchange,
RoutingKey: routingKey,
Publishing: &amqpPublishing}
}
// readSingleMessageFromRawFile reads a single messages from the given io.Reader
// which is typically stdin or a file. If reading from stdin, CTRL+D (linux)
// or CTRL+Z (Win) on an empty line terminates the reader.
func readSingleMessageFromRawFile(reader io.Reader) (amqp.Publishing, error) {
buf := new(bytes.Buffer)
numRead, err := buf.ReadFrom(reader)
if err != nil {
return amqp.Publishing{}, err
} else if numRead == 0 {
return amqp.Publishing{}, io.EOF
}
return amqp.Publishing{Body: buf.Bytes()}, nil
}
// readNextMessageFromJSONStream reads JSON messages from the given decoder as long
// as there are messages available.
func readNextMessageFromJSONStream(decoder *json.Decoder) (amqp.Publishing, error) {
message := RabtapPersistentMessage{}
err := decoder.Decode(&message)
if err != nil {
return amqp.Publishing{}, err
}
return message.ToAmqpPublishing(), nil
}
// createMessageReaderFunc returns a function that reads messages from the
// the given reader in JSON or raw-format
func createMessageReaderFunc(jsonFormat bool, reader io.Reader) MessageReaderFunc {
if jsonFormat {
decoder := json.NewDecoder(reader)
return func() (amqp.Publishing, error) {
return readNextMessageFromJSONStream(decoder)
}
}
return func() (amqp.Publishing, error) {
return readSingleMessageFromRawFile(reader)
}
}
// publishMessages reads messages with the provided readNextMessageFunc and
// publishes the messages to the given exchange.
func publishMessageStream(publishChannel rabtap.PublishChannel,
exchange, routingKey string,
readNextMessageFunc MessageReaderFunc) {
for {
amqpMessage, err := readNextMessageFunc()
switch err {
case io.EOF:
return
case nil:
publishMessage(publishChannel, exchange, routingKey, amqpMessage)
default:
failOnError(err, "error reading message", os.Exit)
}
}
}
// cmdPublish reads messages with the provied readNextMessageFunc and
// publishes the messages to the given exchange.
func cmdPublish(cmd CmdPublishArg) {
log.Debugf("publishing message(s) to exchange %s with routingkey %s",
cmd.exchange, cmd.routingKey)
publisher := rabtap.NewAmqpPublish(cmd.amqpURI, cmd.tlsConfig, log)
defer publisher.Close()
publishChannel := make(rabtap.PublishChannel)
go publisher.EstablishConnection(publishChannel)
publishMessageStream(publishChannel, cmd.exchange, cmd.routingKey,
cmd.readNextMessageFunc)
}