1
0
Fork 0
This commit is contained in:
Gregory Eremin 2014-08-16 16:36:19 +07:00
parent 4f412f0f3c
commit a44339cc34
1 changed files with 107 additions and 0 deletions

107
bin/bsq Executable file
View File

@ -0,0 +1,107 @@
#!/usr/bin/env ruby
require 'optparse'
require 'json'
require 'burlesque'
BANNER = <<-EOL
Burlesque Ruby client v.#{Burlesque::VERSION}
Usage:
Publish:
$ bsq pub urgent "Process this message as soon as possible!"
or
$ cat urgent-183.txt | bsq pub urgent
Subscribe:
$ bsq sub urgent > urgent-184.txt
Status:
$ bsq status
Debug:
$ bsq debug
Options:
EOL
def error msg = nil
unless msg.nil?
puts "\033[31mError: #{msg}\e[0m\n\n"
end
puts $options.help
exit(1)
end
Burlesque.configure
$options = OptionParser.new do |opts|
opts.banner = BANNER
opts.on('-h', '--host [HOST]', 'Burlesque host') do |val|
Burlesque.config.host = val
end
opts.on('-p', '--port [PORT]', Integer, 'Burlesque port') do |val|
Burlesque.config.port = val
end
opts.on('-t', '--timeout [TIMEOUT]', Integer, 'Subscription timeout') do |val|
Burlesque.config.timeout = val
end
opts.on('-v', '--version', 'Display Burlesque Ruby client version') do
puts Burlesque::VERSION
exit(0)
end
opts.on('--help', 'Display this help message') do
puts $options.help
exit(0)
end
end
begin
$options.parse!
rescue OptionParser::InvalidOption => e
error(e.message)
end
client = Burlesque::Client.new
case ARGV[0]
when 'pub'
queue, msg = nil, nil
case ARGV.length
when 3
queue = ARGV[1]
msg = ARGV[2]
when 2
queue = ARGV[1]
msg = STDIN.read
else
error
end
ok = client.publish(queue, msg)
exit(ok ? 0 : 1)
when 'sub'
queue = nil
if ARGV.length == 2
queue = ARGV[1]
else
error
end
res = client.subscribe(queue)
if res.nil?
exit(1)
else
puts res[:message]
end
when 'status'
puts JSON.pretty_generate(client.status)
when 'debug'
puts JSON.pretty_generate(client.status)
else
error
end