Class: AmqpUtils::Command
- Inherits:
-
Object
- Object
- AmqpUtils::Command
- Includes:
- Clio::Terminal
- Defined in:
- lib/amqp_utils/command.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #amqp ⇒ Object
- #channel ⇒ Object (also: #mq)
- #command_name ⇒ Object
- #go ⇒ Object
-
#initialize(args) ⇒ Command
constructor
A new instance of Command.
- #process_options ⇒ Object
-
#validate ⇒ Object
Called to validate that the supplied command line options and arguments are valid.
- #version ⇒ Object
Constructor Details
#initialize(args) ⇒ Command
Returns a new instance of Command.
22 23 24 |
# File 'lib/amqp_utils/command.rb', line 22 def initialize(args) @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
30 31 32 |
# File 'lib/amqp_utils/command.rb', line 30 def args @args end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
30 31 32 |
# File 'lib/amqp_utils/command.rb', line 30 def @options end |
Class Method Details
.run(args = ARGV) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/amqp_utils/command.rb', line 7 def run(args = ARGV) command = new(args) command. command.validate begin command.go rescue => e STDERR.puts e. STDERR.puts e.backtrace.join("\n") if command..verbose exit 1 end end |
Instance Method Details
#amqp ⇒ Object
101 102 103 |
# File 'lib/amqp_utils/command.rb', line 101 def amqp @amqp ||= AMQP.start end |
#channel ⇒ Object Also known as: mq
105 106 107 |
# File 'lib/amqp_utils/command.rb', line 105 def channel @channel ||= AMQP::Channel.new(@amqp) end |
#command_name ⇒ Object
65 66 67 |
# File 'lib/amqp_utils/command.rb', line 65 def command_name File.basename($0) end |
#go ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/amqp_utils/command.rb', line 69 def go if .prompt [:password] = password() puts end %w(host port vhost user timeout).each do |val| AMQP.settings[val.to_sym] = [val.to_sym] end AMQP.settings[:pass] = .password AMQP.logging = .verbose trap("INT") do if @nice_tried EM.stop else AMQP.stop { EM.stop } @nice_tried = true end end EM.run do amqp.connection_status do |status| if status == :disconnected Trollop::die "disconnected from #{AMQP.settings[:host]}:#{AMQP.settings[:port]}" end end mq.callback { execute } end end |
#process_options ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/amqp_utils/command.rb', line 32 def command = self @options = Trollop::(@args) do version(command.version) command.(self) if command.respond_to?(:prepare_options) <<-END.unindent Standard options: END opt :host, 'The AMQP host to connect to', :short => 'H', :default => 'localhost' opt :port, 'The AMQP port to connect to', :short => 'P', :default => 5672 opt :vhost, 'The vhost to connect to', :short => 'V', :default => '/' opt :user, 'The user name to authenticate with', :default => 'guest', :short => 'u' opt :prompt, 'Prompt the user for a password', :short => 'p' opt :password, 'The password to connect with.', :default => 'guest', :short => :none conflicts(:prompt, :password) opt :timeout, 'The connect timeout in seconds', :default => 5, :short => 't' opt :verbose, 'Print all AMQP commands sent and received.', :short => 'v' end @args = @args.dup ARGV.clear end |
#validate ⇒ Object
Called to validate that the supplied command line options and arguments are valid. If there is a problem with the supplied values, Trollop::die should be called.
Subclasses should override this method and do their validation.
62 63 |
# File 'lib/amqp_utils/command.rb', line 62 def validate end |
#version ⇒ Object
26 27 28 |
# File 'lib/amqp_utils/command.rb', line 26 def version IO.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION')) end |