Skip to content

Commit

Permalink
updated README & some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hassansin committed May 24, 2017
1 parent 8a008f6 commit 0e10d72
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 40 deletions.
30 changes: 8 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
## amqptools
# amqptools

A brief description of your application

### Synopsis


A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

### Options
## Installing

```
-h, --help help for amqptools
go get -u github.com/hassansin/amqptools
```

### SEE ALSO
* [amqptools consume](#amqptools-consume) - Consumes messages
* [amqptools publish](#amqptools-publish) - Publishes a message
## Usage

## amqptools consume

Expand All @@ -28,7 +18,8 @@ Consumes messages
Consume messages
Uses the default exchange '', When no exchange is provided
Use comma-separated values for binding the same queue with multiple routing keys:
amqptools consume --exchange logs --keys info,warning,debug

amqptools consume --exchange logs --keys info,warning,debug



Expand Down Expand Up @@ -64,9 +55,6 @@ amqptools consume [flags]
-h, --help help for consume
```

### SEE ALSO
* [amqptools](#amqptools) - A brief description of your application

## amqptools publish

Publishes a message
Expand All @@ -76,7 +64,8 @@ Publishes a message

Publish a message using exchange and routing key.
mesage can be string or stdin:
echo 'hello world' | amqptools publish --exchange=logs --key=info

echo 'hello world' | amqptools publish --exchange=logs --key=info



Expand Down Expand Up @@ -109,6 +98,3 @@ amqptools publish [flags] [message]
-h, --help help for publish
```

### SEE ALSO
* [amqptools](#amqptools) - A brief description of your application

46 changes: 40 additions & 6 deletions cmd/consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var consumeCmd = &cobra.Command{
Long: `Consume messages
Uses the default exchange '', When no exchange is provided
Use comma-separated values for binding the same queue with multiple routing keys:
amqptools consume --exchange logs --keys info,warning,debug
amqptools consume --exchange logs --keys info,warning,debug
`,
Example: ` ampqtool consume -H ampq.example.com -P 5672 --exchange amq.direct --durable-queue
Expand Down Expand Up @@ -49,18 +50,39 @@ Use comma-separated values for binding the same queue with multiple routing keys
}
closes := ch.NotifyClose(make(chan *amqp.Error, 1))

q, err := ch.QueueDeclare(queue, durableQueue, false, exclusive, false, nil)
q, err := ch.QueueDeclare(
queue, // queue
durableQueue, // durable
false, // auto-delete
exclusive, // exclusive
false, // no-wait
nil, // args
)
if err != nil {
return fmt.Errorf("queue.declare: %v", err)
}
// bind queue for non-default exchange
if exchange != "" {
if err = ch.ExchangeDeclare(exchange, exchangeType, durableExchange, false, false, false, nil); err != nil {
if err = ch.ExchangeDeclare(
exchange, // name
exchangeType, // type
durableExchange, // durable
false, // auto-delete
false, // internal
false, // no-wait
nil, // args
); err != nil {
return fmt.Errorf("exchange.declare: %v", err)
}
// bind the queue to all routingkeys
for _, key := range strings.Split(routingkey, ",") {
if err = ch.QueueBind(queue, key, exchange, false, nil); err != nil {
if err = ch.QueueBind(
queue, // queue
key, // routing-key
exchange, //exchange
false, // no-wait
nil, // args
); err != nil {
return fmt.Errorf("queue.bind: %v", err)
}
}
Expand All @@ -70,11 +92,23 @@ Use comma-separated values for binding the same queue with multiple routing keys
if number > 0 {
prefetchCount = number
}
if err = ch.Qos(prefetchCount, 0, false); err != nil {
if err = ch.Qos(
prefetchCount, // prefetch count
0, // prefetch size
false, // global
); err != nil {
return fmt.Errorf("basic.qos: %v", err)
}

msgs, err := ch.Consume(queue, "", !noAck, exclusive, false, false, nil)
msgs, err := ch.Consume(
queue, // queue
"", // consumer id
!noAck, // auto-ack
exclusive, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
return fmt.Errorf("basic.consume: %v", err)
}
Expand Down
19 changes: 13 additions & 6 deletions cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ var docCmd = &cobra.Command{
cmd.SilenceUsage = true
cmd.SilenceErrors = true

rootDoc := new(bytes.Buffer)
if err := doc.GenMarkdownCustom(RootCmd, rootDoc, linkHandler); err != nil {
return err
}

consumeDoc := new(bytes.Buffer)
if err := doc.GenMarkdownCustom(consumeCmd, consumeDoc, linkHandler); err != nil {
return err
Expand All @@ -44,9 +39,21 @@ var docCmd = &cobra.Command{
return err
}
defer f.Close()
if _, err = f.Write(rootDoc.Bytes()); err != nil {

if _, err = f.WriteString(`# amqptools
## Installing
` + "```" + `
go get -u github.com/hassansin/amqptools
` + "```" + `
## Usage
`); err != nil {
return err
}

if _, err = f.Write(consumeDoc.Bytes()); err != nil {
return err
}
Expand Down
21 changes: 18 additions & 3 deletions cmd/produce.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var produceCmd = &cobra.Command{
Short: "Publishes a message",
Long: `Publish a message using exchange and routing key.
mesage can be string or stdin:
echo 'hello world' | amqptools publish --exchange=logs --key=info
echo 'hello world' | amqptools publish --exchange=logs --key=info
`,
Example: ` ampqtools publish -H ampq.example.com -P 5672 --exchange=amq.direct --key=hello "hello world"
Expand Down Expand Up @@ -55,7 +56,15 @@ mesage can be string or stdin:
return fmt.Errorf("channel.open: %v", err)
}
if exchange != "" {
if err = ch.ExchangeDeclare(exchange, exchangeType, durableExchange, false, false, false, nil); err != nil {
if err = ch.ExchangeDeclare(
exchange, // name
exchangeType, // type
durableExchange, // durable
false, // auto-delete
false, // internal
false, // no-wait
nil, // args
); err != nil {
return fmt.Errorf("exchange.declare: %v", err)
}
}
Expand All @@ -80,7 +89,13 @@ mesage can be string or stdin:
returns := ch.NotifyReturn(make(chan amqp.Return, 1))
confirms := ch.NotifyPublish(make(chan amqp.Confirmation, 1))

if err = ch.Publish(exchange, routingkey, true, false, msg); err != nil {
if err = ch.Publish(
exchange, // exchange
routingkey, // routing-key
true, // mandatory
false, // immediate
msg, // message
); err != nil {
return fmt.Errorf("basic.publish: %v", err)
}
select {
Expand Down
4 changes: 1 addition & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ var properties = table{amqp.Table{}, true}
var RootCmd = &cobra.Command{
Use: "amqptools",
DisableAutoGenTag: true,
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:`,
Short: "Consume or publish messages",
}

func commonFlagSet() *pflag.FlagSet {
Expand Down

0 comments on commit 0e10d72

Please sign in to comment.