Skip to content

Commit 974fe19

Browse files
author
Olivier Laurendeau
authored
Merge pull request #2 from olaurendeau/config_file
Refactore & Users can define a .tailmq file in their home dir with a list of servers
2 parents 3e033c1 + 153d056 commit 974fe19

File tree

7 files changed

+289
-147
lines changed

7 files changed

+289
-147
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
bin/
1717
src/
1818
pkg/
19+
build/

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
build: gopath gobin install
1+
build: build-darwin-amd64 build-linux-amd64
22

33
install:
44
go install
55

6-
gopath:
7-
export GOPATH="$(PWD)"
6+
build-darwin-amd64:
7+
GOOS=darwin GOARCH=amd64 go build -o build/tailmq-darwin-amd64
88

9-
gobin:
10-
export GOBIN="$(PWD)/bin"
9+
build-linux-amd64:
10+
GOOS=linux GOARCH=amd64 go build -o build/tailmq-linux-amd64

README.md

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ Tail messages from a RabbitMQ exchange into your CLI console
33

44
# Installation
55

6+
## Linux
7+
8+
```bash
9+
curl -O https://github.com/olaurendeau/tailmq/releases/download/v1.0.0/tailmq-linux-amd64
10+
mv tailmq-linux-amd64 /usr/local/bin/tailmq
11+
rm tailmq-linux-amd64
12+
```
13+
14+
## MacOS
15+
16+
```bash
17+
curl -O https://github.com/olaurendeau/tailmq/releases/download/v1.0.0/tailmq-darwin-amd64
18+
sudo mv tailmq-darwin-amd64 /usr/local/bin/tailmq
19+
rm tailmq-darwin-amd64
20+
```
21+
622
# Usage examples
723

824
Dump messages from an exchange to your console
@@ -35,22 +51,45 @@ $ tailmq amq.topic | grep sent
3551

3652
```bash
3753
$ tailmq --help
38-
NAME:
39-
tailmq - Tail a RabbitMQ exchange
54+
DESCRIPTION
55+
TailMQ tail AMQP exchanges and output messages in stdout
56+
USAGE
57+
tailmq [options] <exchange_name>
58+
EXAMPLES
59+
tailmq amp.direct - Tail exchange amp.direct on local server with default access
60+
tailmq -uri=amqp://user:[email protected]:5672//awesome amp.topic - Tail exchange amp.topic from server tailmq.com in vhost /awesome
61+
tailmq -server=prod amp.fanout - Tail exchange amp.fanout from server prod configured in file ~/.tailmq
62+
tailmq -server=prod -vhost=/foobar amp.fanout - Tail exchange amp.fanout from server prod configured in file ~/.tailmq but use vhost /foobar
63+
OPTIONS
64+
-help
65+
How does it work ?
66+
-prefix
67+
Should output be prefixed with datetime and time
68+
-server string
69+
Use predefined server from configuration
70+
-uri string
71+
RabbitMQ amqp uri (default "amqp://guest:guest@localhost:5672/")
72+
-verbose
73+
Do you want more informations ?
74+
-vhost string
75+
Define vhost to tail from
76+
```
4077

41-
USAGE:
42-
tailmq [global options] command [command options] [exchangeName]
78+
# Config file
4379

44-
VERSION:
45-
0.1.0
80+
## Format
4681

47-
COMMANDS:
48-
help, h Shows a list of commands or help for one command
82+
```yaml
83+
servers:
84+
server_name: amqp_uri
85+
```
86+
87+
## Sample
4988
50-
GLOBAL OPTIONS:
51-
--uri value, -u value RabbitMQ amqp uri (default: "amqp://guest:guest@localhost:5672/")
52-
--prefix Should output be prefixed with date and time
53-
--verbose Do you want more informations ?
54-
--help, -h show help
55-
--version print only the version
56-
```
89+
```yaml
90+
servers:
91+
local: amqp://localhost:5672/
92+
staging: amqp://staging.tailmq.io:5672/
93+
staging_the_vhost: amqp://staging.tailmq.io:5672/the_vhost
94+
prod: amqp://tailmq.io:5672/
95+
```

config/config.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

consumer/consumer.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package consumer
2+
3+
import (
4+
"log"
5+
"github.com/satori/go.uuid"
6+
"github.com/streadway/amqp"
7+
)
8+
9+
type Consumer struct {
10+
uri string
11+
exchangeName string
12+
conn *amqp.Connection
13+
ch *amqp.Channel
14+
Deliveries <-chan amqp.Delivery
15+
Err chan error
16+
}
17+
18+
func New(uri string, exchangeName string) *Consumer {
19+
c := &Consumer{}
20+
c.uri = uri
21+
c.exchangeName = exchangeName
22+
c.Err = make(chan error)
23+
24+
return c
25+
}
26+
27+
func (c *Consumer) Start() () {
28+
var err error
29+
30+
c.conn, c.ch = c.openChannel()
31+
32+
q := c.createExpirableQueue(c.ch)
33+
34+
c.Deliveries, err = c.ch.Consume(q.Name, "", true, false, false, false, nil)
35+
c.Err <- err
36+
}
37+
38+
func (c *Consumer) Stop() {
39+
c.conn.Close()
40+
c.ch.Close()
41+
}
42+
43+
func (c *Consumer) openChannel() (*amqp.Connection, *amqp.Channel) {
44+
log.Printf("Establishing connection... "+ c.uri)
45+
conn, err := amqp.Dial(c.uri)
46+
c.Err <- err
47+
log.Printf("Connected")
48+
49+
ch, err := conn.Channel()
50+
c.Err <- err
51+
log.Printf("Channel opened")
52+
53+
return conn, ch
54+
}
55+
56+
func (c *Consumer) createExpirableQueue(ch *amqp.Channel) (amqp.Queue) {
57+
var args amqp.Table
58+
args = make(amqp.Table)
59+
args["x-expires"] = int32(10000)
60+
61+
q, err := ch.QueueDeclare("tailmq_"+uuid.NewV4().String(), false, true, false, false, args)
62+
c.Err <- err
63+
log.Printf("Queue defined")
64+
65+
err = ch.QueueBind(q.Name, "#", c.exchangeName, false, nil)
66+
c.Err <- err
67+
err = ch.QueueBind(q.Name, "", c.exchangeName, false, nil)
68+
c.Err <- err
69+
log.Printf("Queue " + q.Name + " binded to exchange "+c.exchangeName)
70+
71+
return q
72+
}

global_config.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"errors"
6+
"os/user"
7+
8+
"gopkg.in/yaml.v2"
9+
)
10+
11+
type GlobalConfig struct {
12+
Servers map[string]string
13+
}
14+
15+
func NewGlobalConfig(path string) (GlobalConfig, error) {
16+
17+
var globalConfig GlobalConfig
18+
19+
if path == "" {
20+
usr, err := user.Current()
21+
if err != nil {
22+
return globalConfig, err
23+
}
24+
25+
path = usr.HomeDir + "/.tailmq"
26+
}
27+
28+
fileContent, err := ioutil.ReadFile(path)
29+
30+
if err != nil {
31+
return globalConfig, err
32+
}
33+
34+
err = yaml.Unmarshal(fileContent, &globalConfig)
35+
36+
return globalConfig, err
37+
}
38+
39+
func (g GlobalConfig) getServerUri(server string) (string, error) {
40+
var err error
41+
uri := g.Servers[server]
42+
if uri == "" {
43+
err = errors.New("No server named " + server)
44+
}
45+
46+
return uri, err
47+
}

0 commit comments

Comments
 (0)