Skip to content
This repository was archived by the owner on Mar 6, 2023. It is now read-only.

Commit 6f3530d

Browse files
committed
first commit
1 parent 0ea42c9 commit 6f3530d

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# amqp-publish
2+
3+
A simple tool to publish messages to RabbitMQ from the command line.
4+
5+
## Setup
6+
7+
Download the latest release binary and save it to `/usr/local/bin` or any executable path.
8+
9+
## Usage
10+
11+
Publish to exchange
12+
13+
```SHELL
14+
amqp-publish --uri="amqp://admin:password@localhost:5672/" --exchange="foo" --routing-key="awesome-routing-key" --body="hello, world!"
15+
```
16+
17+
Publish the `bar` queue directly, using RabbitMQ default exchange
18+
19+
```SHELL
20+
amqp-publish --uri="amqp://admin:password@localhost:5672/" --exchange="" --routing-key="bar" --body="hello, world!"
21+
```
22+
23+
Cry for help
24+
25+
```SHELL
26+
amqp-publish --help
27+
```
28+
29+
## Credit
30+
31+
Streadway's awesome [AMQP Go library](github.com/streadway/amqp).

main.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"flag"
7+
"errors"
8+
"github.com/streadway/amqp"
9+
)
10+
11+
var (
12+
uri string
13+
exchange string
14+
routingKey string
15+
body string
16+
)
17+
18+
func validateFlags() error {
19+
if uri == "" {
20+
return errors.New("uri cannot be blank")
21+
}
22+
23+
if exchange == "" && routingKey == "" {
24+
return errors.New("exchange and routing-key cannot both be blank")
25+
}
26+
27+
if body == "" {
28+
return errors.New("body cannot be blank")
29+
}
30+
31+
return nil
32+
}
33+
34+
func init() {
35+
flag.StringVar(&uri, "uri", "", "AMQP URI amqp://<user>:<password>@<host>:<port>/[vhost]")
36+
flag.StringVar(&exchange, "exchange", "", "Exchange name")
37+
flag.StringVar(&routingKey, "routing-key", "", `Routing key. Use queue
38+
name with blank exchange to publish directly to queue.`)
39+
flag.StringVar(&body, "body", "", "Message body")
40+
41+
flag.Parse()
42+
43+
err := validateFlags()
44+
45+
if err != nil {
46+
fmt.Println(err)
47+
os.Exit(1)
48+
}
49+
}
50+
51+
func main() {
52+
connection, err := amqp.Dial(uri)
53+
54+
if err != nil {
55+
fmt.Println(err)
56+
os.Exit(1)
57+
}
58+
59+
defer connection.Close()
60+
61+
channel, _ := connection.Channel()
62+
63+
channel.Publish(exchange, routingKey, false, false, amqp.Publishing{
64+
Headers: amqp.Table{},
65+
ContentType: "text/plain",
66+
ContentEncoding: "",
67+
Body: []byte(body),
68+
DeliveryMode: amqp.Transient,
69+
Priority: 0,
70+
})
71+
}

0 commit comments

Comments
 (0)