#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../lib/amqp_utils/command'

class DequeueCommand < AmqpUtils::Command
  def prepare_options(options)
    options.banner %Q{
    |Removes messages from the supplied queues and displays them on STDOUT.
    |
    |  #{command_name} <queue> [<another queue> ...]
    }.margin
  end

  def validate
    raise "need at least one queue name" if args.empty?
  end

  def execute
    @queues = args
    @queues.each do |queue|
      puts "Dequeueing from #{queue} (^C to stop)..."
      mq = MQ.new

      process_message = lambda do |header, message|
        puts "(#{queue})"
        puts "  Header: #{header.properties.inspect}"
        puts "  Message: #{message.inspect}"
      end
      mq.queue(queue).subscribe &process_message
    end
  end
end

DequeueCommand.run
