-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from olaurendeau/config_file
Refactore & Users can define a .tailmq file in their home dir with a list of servers
- Loading branch information
Showing
7 changed files
with
289 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ | |
bin/ | ||
src/ | ||
pkg/ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
build: gopath gobin install | ||
build: build-darwin-amd64 build-linux-amd64 | ||
|
||
install: | ||
go install | ||
|
||
gopath: | ||
export GOPATH="$(PWD)" | ||
build-darwin-amd64: | ||
GOOS=darwin GOARCH=amd64 go build -o build/tailmq-darwin-amd64 | ||
|
||
gobin: | ||
export GOBIN="$(PWD)/bin" | ||
build-linux-amd64: | ||
GOOS=linux GOARCH=amd64 go build -o build/tailmq-linux-amd64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,22 @@ Tail messages from a RabbitMQ exchange into your CLI console | |
|
||
# Installation | ||
|
||
## Linux | ||
|
||
```bash | ||
curl -O https://github.com/olaurendeau/tailmq/releases/download/v1.0.0/tailmq-linux-amd64 | ||
mv tailmq-linux-amd64 /usr/local/bin/tailmq | ||
rm tailmq-linux-amd64 | ||
``` | ||
|
||
## MacOS | ||
|
||
```bash | ||
curl -O https://github.com/olaurendeau/tailmq/releases/download/v1.0.0/tailmq-darwin-amd64 | ||
sudo mv tailmq-darwin-amd64 /usr/local/bin/tailmq | ||
rm tailmq-darwin-amd64 | ||
``` | ||
|
||
# Usage examples | ||
|
||
Dump messages from an exchange to your console | ||
|
@@ -35,22 +51,45 @@ $ tailmq amq.topic | grep sent | |
|
||
```bash | ||
$ tailmq --help | ||
NAME: | ||
tailmq - Tail a RabbitMQ exchange | ||
DESCRIPTION | ||
TailMQ tail AMQP exchanges and output messages in stdout | ||
USAGE | ||
tailmq [options] <exchange_name> | ||
EXAMPLES | ||
tailmq amp.direct - Tail exchange amp.direct on local server with default access | ||
tailmq -uri=amqp://user:[email protected]:5672//awesome amp.topic - Tail exchange amp.topic from server tailmq.com in vhost /awesome | ||
tailmq -server=prod amp.fanout - Tail exchange amp.fanout from server prod configured in file ~/.tailmq | ||
tailmq -server=prod -vhost=/foobar amp.fanout - Tail exchange amp.fanout from server prod configured in file ~/.tailmq but use vhost /foobar | ||
OPTIONS | ||
-help | ||
How does it work ? | ||
-prefix | ||
Should output be prefixed with datetime and time | ||
-server string | ||
Use predefined server from configuration | ||
-uri string | ||
RabbitMQ amqp uri (default "amqp://guest:guest@localhost:5672/") | ||
-verbose | ||
Do you want more informations ? | ||
-vhost string | ||
Define vhost to tail from | ||
``` | ||
|
||
USAGE: | ||
tailmq [global options] command [command options] [exchangeName] | ||
# Config file | ||
|
||
VERSION: | ||
0.1.0 | ||
## Format | ||
|
||
COMMANDS: | ||
help, h Shows a list of commands or help for one command | ||
```yaml | ||
servers: | ||
server_name: amqp_uri | ||
``` | ||
## Sample | ||
GLOBAL OPTIONS: | ||
--uri value, -u value RabbitMQ amqp uri (default: "amqp://guest:guest@localhost:5672/") | ||
--prefix Should output be prefixed with date and time | ||
--verbose Do you want more informations ? | ||
--help, -h show help | ||
--version print only the version | ||
``` | ||
```yaml | ||
servers: | ||
local: amqp://localhost:5672/ | ||
staging: amqp://staging.tailmq.io:5672/ | ||
staging_the_vhost: amqp://staging.tailmq.io:5672/the_vhost | ||
prod: amqp://tailmq.io:5672/ | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package consumer | ||
|
||
import ( | ||
"log" | ||
"github.com/satori/go.uuid" | ||
"github.com/streadway/amqp" | ||
) | ||
|
||
type Consumer struct { | ||
uri string | ||
exchangeName string | ||
conn *amqp.Connection | ||
ch *amqp.Channel | ||
Deliveries <-chan amqp.Delivery | ||
Err chan error | ||
} | ||
|
||
func New(uri string, exchangeName string) *Consumer { | ||
c := &Consumer{} | ||
c.uri = uri | ||
c.exchangeName = exchangeName | ||
c.Err = make(chan error) | ||
|
||
return c | ||
} | ||
|
||
func (c *Consumer) Start() () { | ||
var err error | ||
|
||
c.conn, c.ch = c.openChannel() | ||
|
||
q := c.createExpirableQueue(c.ch) | ||
|
||
c.Deliveries, err = c.ch.Consume(q.Name, "", true, false, false, false, nil) | ||
c.Err <- err | ||
} | ||
|
||
func (c *Consumer) Stop() { | ||
c.conn.Close() | ||
c.ch.Close() | ||
} | ||
|
||
func (c *Consumer) openChannel() (*amqp.Connection, *amqp.Channel) { | ||
log.Printf("Establishing connection... "+ c.uri) | ||
conn, err := amqp.Dial(c.uri) | ||
c.Err <- err | ||
log.Printf("Connected") | ||
|
||
ch, err := conn.Channel() | ||
c.Err <- err | ||
log.Printf("Channel opened") | ||
|
||
return conn, ch | ||
} | ||
|
||
func (c *Consumer) createExpirableQueue(ch *amqp.Channel) (amqp.Queue) { | ||
var args amqp.Table | ||
args = make(amqp.Table) | ||
args["x-expires"] = int32(10000) | ||
|
||
q, err := ch.QueueDeclare("tailmq_"+uuid.NewV4().String(), false, true, false, false, args) | ||
c.Err <- err | ||
log.Printf("Queue defined") | ||
|
||
err = ch.QueueBind(q.Name, "#", c.exchangeName, false, nil) | ||
c.Err <- err | ||
err = ch.QueueBind(q.Name, "", c.exchangeName, false, nil) | ||
c.Err <- err | ||
log.Printf("Queue " + q.Name + " binded to exchange "+c.exchangeName) | ||
|
||
return q | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"errors" | ||
"os/user" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type GlobalConfig struct { | ||
Servers map[string]string | ||
} | ||
|
||
func NewGlobalConfig(path string) (GlobalConfig, error) { | ||
|
||
var globalConfig GlobalConfig | ||
|
||
if path == "" { | ||
usr, err := user.Current() | ||
if err != nil { | ||
return globalConfig, err | ||
} | ||
|
||
path = usr.HomeDir + "/.tailmq" | ||
} | ||
|
||
fileContent, err := ioutil.ReadFile(path) | ||
|
||
if err != nil { | ||
return globalConfig, err | ||
} | ||
|
||
err = yaml.Unmarshal(fileContent, &globalConfig) | ||
|
||
return globalConfig, err | ||
} | ||
|
||
func (g GlobalConfig) getServerUri(server string) (string, error) { | ||
var err error | ||
uri := g.Servers[server] | ||
if uri == "" { | ||
err = errors.New("No server named " + server) | ||
} | ||
|
||
return uri, err | ||
} |
Oops, something went wrong.