#!/usr/bin/env ruby $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))) require 'rubygems' require 'bundler/setup' require 'shellwords' require 'dap' def version $stderr.puts "dap #{Dap::VERSION}" exit(0) end def usage $stderr.puts "" $stderr.puts " Usage: #{$0} [input] + [filter] + [output]" $stderr.puts " --inputs" $stderr.puts " --outputs" $stderr.puts " --filters" $stderr.puts "" $stderr.puts "Example: echo world | #{$0} lines stdin + rename line=hello + json stdout" $stderr.puts "" exit(1) end def show_inputs $stderr.puts "Inputs:" Dap::Factory.inputs.each_pair do |k,v| $stderr.puts " * #{k}" end $stderr.puts exit(1) end def show_outputs $stderr.puts "Outputs:" Dap::Factory.outputs.each_pair do |k,v| $stderr.puts " * #{k}" end $stderr.puts exit(1) end def show_filters $stderr.puts "Filters:" Dap::Factory.filters.each_pair do |k,v| $stderr.puts " * #{k}" end $stderr.puts exit(1) end trace = false args = [] # # Tokenize on + then treat each stage as a separate name + argument list # ARGV.join(' ').split(/\s*\+\s*/).each do |bit| # Handle quoted arguments as needed # XXX: Doesn't work as expected since ARGV parsing gobbles them up aset = Shellwords.shellwords(bit) # Check the first argument for help or usage flags arg = aset.first if arg == "--trace" trace = true arg = aset.shift end if arg == "-h" or arg == "--help" usage end if arg == "--version" or arg == "-v" version end if arg == "--inputs" show_inputs end if arg == "--outputs" show_outputs end if arg == "--filters" show_filters end args << aset if aset.length > 0 end inp_args = args.shift out_args = args.pop usage if (inp_args == nil or out_args == nil) filters = [] inp = Dap::Factory.create_input(inp_args) out = Dap::Factory.create_output(out_args) args.each do |a| filters << Dap::Factory.create_filter(a) end out.start while true data = inp.read_record break if data == Dap::Input::Error::EOF next if data == Dap::Input::Error::Empty next if data == Dap::Input::Error::InvalidFormat docs = [ data ] fcount = 1 filters.each do |f| $stderr.puts "T: #{" " * (fcount * 2)}#{f.name} -> #{docs.inspect} " if trace docs = docs.collect {|doc| f.process(doc) }.flatten $stderr.puts "T: #{" " * (fcount * 2)}#{" " * f.name.length} == #{docs.inspect}" if trace fcount += 1 break if docs.length == 0 end begin docs.each do |doc| out.write_record(doc) end rescue ::Errno::EPIPE break end end out.stop