Created
May 15, 2015 01:46
-
-
Save sykesm/0e1c7709fa282582036d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'rubygems' | |
require 'nats/client' | |
def command_available?(cmd) | |
`command -v #{cmd} >/dev/null 2>&1` | |
$?.success? | |
end | |
def decorate(subject, msg) | |
$i=0 unless $i | |
$i += 1 | |
# puts "[\##{$i+=1}] [#{subject}]" | |
if command_available?("jq") && $stdout.tty? | |
puts `echo '[{\"nats:idx\": \"#{$i}\", \"nats:subject\": \"#{subject}\"}, #{msg}]' | jq -C '.[0]+.[1]'` | |
else | |
puts msg | |
end | |
end | |
['TERM', 'INT'].each { |s| trap(s) { puts; exit! } } | |
nats_server = "nats://nats:[email protected]:4222" | |
subject = ARGV.dup.shift || '>' | |
NATS.on_error { |err| puts "Server Error: #{err}"; exit! } | |
NATS.start(:uri => nats_server, :autostart => true) do | |
puts "Listening on [#{subject}]" unless $show_raw | |
NATS.subscribe(subject) { |msg, _, sub| decorate(sub, msg) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment