class Turnstile::CLI::Launcher

Attributes

options[RW]
sinatra_thread[R]
stderr[R]
stdin[R]
stdout[R]

Public Class Methods

new(options, stdin = STDIN, stdout = STDOUT, stderr = STDERR) click to toggle source
# File lib/turnstile/cli/launcher.rb, line 11
def initialize(options, stdin = STDIN, stdout = STDOUT, stderr = STDERR)
  self.options            = options
  @stdin, @stdout, @stderr= stdin, stdout, stderr
end

Public Instance Methods

command(name) click to toggle source
# File lib/turnstile/cli/launcher.rb, line 58
def command(name)
  ::Turnstile::Commands.command(name).new(options)
end
handle_error(title, e) click to toggle source
# File lib/turnstile/cli/launcher.rb, line 62
def handle_error(title, e)
  if options[:trace]
    trace = e.backtrace.reverse
    last  = trace.pop
    stderr.puts trace.join("\n")
    stderr.puts last.bold.red
  end
  stderr.puts
  stderr.puts title.bold.yellow
  stderr.puts "\t" + e.message.red
  stderr.puts
end
launch() click to toggle source
# File lib/turnstile/cli/launcher.rb, line 16
def launch
  launch_sinatra_app if options[:web]
  launch_signal_handler

  tdb "config: #{config.to_h}" if Turnstile.config.trace
  result = if options[:show]
             command(:show).execute(options[:show_format] || :json, options[:delimiter])

           elsif options[:token]
             tracker.track_token(options[:token], options[:delimiter])

           elsif options[:flushdb]
             command(:flushdb).execute

           elsif options[:print_keys]
             command(:print_keys).execute

           elsif options[:file]
             controller.start
           end


  puts result if result && !result.empty?
rescue SystemExit, SignalException
  exit 6
rescue Exception => e
  handle_error('Error', e)
ensure
  sinatra_thread.join if sinatra_thread
end
launch_signal_handler() click to toggle source
# File lib/turnstile/cli/launcher.rb, line 47
def launch_signal_handler
  Signal.trap('INT') { sleep 1; Kernel.exit(5) }
end
launch_sinatra_app() click to toggle source
# File lib/turnstile/cli/launcher.rb, line 51
def launch_sinatra_app
  @sinatra_thread = Thread.new do
    require_relative '../web_app'
    Kernel.exit(0)
  end
end

Private Instance Methods

controller() click to toggle source
# File lib/turnstile/cli/launcher.rb, line 77
def controller
  @controller ||= Turnstile::Collector::Controller.new(options)
end