Class: AmqpUtils::Command

Inherits:
Object
  • Object
show all
Includes:
Clio::Terminal
Defined in:
lib/amqp_utils/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#argsObject (readonly)

Returns the value of attribute args.



30
31
32
# File 'lib/amqp_utils/command.rb', line 30

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



30
31
32
# File 'lib/amqp_utils/command.rb', line 30

def options
  @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.process_options
  command.validate

  begin
    command.go
  rescue => e
    STDERR.puts e.message
    STDERR.puts e.backtrace.join("\n") if command.options.verbose
    exit 1
  end
end

Instance Method Details

#amqpObject



101
102
103
# File 'lib/amqp_utils/command.rb', line 101

def amqp
  @amqp ||= AMQP.start
end

#channelObject Also known as: mq



105
106
107
# File 'lib/amqp_utils/command.rb', line 105

def channel
  @channel ||= AMQP::Channel.new(@amqp)
end

#command_nameObject



65
66
67
# File 'lib/amqp_utils/command.rb', line 65

def command_name
  File.basename($0)
end

#goObject



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 options.prompt
    options[:password] = password()
    puts
  end

  %w(host port vhost user timeout).each do |val|
    AMQP.settings[val.to_sym] = options[val.to_sym]
  end
  AMQP.settings[:pass] = options.password
  AMQP.logging = options.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_optionsObject



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 process_options
  command = self
  @options = Trollop::options(@args) do
    version(command.version)
    command.prepare_options(self) if command.respond_to?(:prepare_options)

    banner <<-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

#validateObject

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

#versionObject



26
27
28
# File 'lib/amqp_utils/command.rb', line 26

def version
  IO.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION'))
end