Method: JDC::Cli::Runner#run

Defined in:
lib/cli/runner.rb

#runObject



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/cli/runner.rb', line 421

def run

  trap('TERM') { print "\nTerminated\n"; exit(false)}

  parse_options!

  @options[:colorize] = false unless STDOUT.tty?

  JDC::Cli::Config.colorize   = @options.delete(:colorize)
  JDC::Cli::Config.nozip      = @options.delete(:nozip)
  JDC::Cli::Config.trace      = @options.delete(:trace)
  JDC::Cli::Config.output   ||= STDOUT unless @options[:quiet]

  process_aliases!
  parse_command!

  if @namespace && @action
    cmd = JDC::Cli::Command.const_get(@namespace.to_s.capitalize)
    cmd.new(@options).send(@action, *@args.collect(&:dup))
  elsif @help_only || @usage
    display_usage
  else
    display basic_usage
    exit(false)
  end

rescue OptionParser::InvalidOption => e
  puts(e.message.red)
  puts("\n")
  puts(basic_usage)
  @exit_status = false
rescue OptionParser::AmbiguousOption => e
  puts(e.message.red)
  puts("\n")
  puts(basic_usage)
  @exit_status = false
rescue JDC::Client::AuthError => e
  if JDC::Cli::Config.auth_token.nil?
    puts "Login Required".red
  else
    puts "Not Authorized".red
  end
  @exit_status = false
rescue JDC::Client::TargetError, JDC::Client::NotFound, JDC::Client::BadTarget  => e
  puts e.message.red
  @exit_status = false
rescue JDC::Client::HTTPException => e
  puts e.message.red
  @exit_status = false
rescue JDC::Cli::GracefulExit => e
  # Redirected commands end up generating this exception (kind of goto)
rescue JDC::Cli::CliExit => e
  puts e.message.red
  @exit_status = false
rescue JDC::Cli::CliError => e
  say("Error #{e.error_code}: #{e.message}".red)
  @exit_status = false
rescue SystemExit => e
  @exit_status = e.success?
rescue SyntaxError => e
  puts e.message.red
  puts e.backtrace
  @exit_status = false
rescue Interrupt => e
  say("\nInterrupted".red)
  @exit_status = false
rescue Exception => e
  puts e.message.red
  puts e.backtrace
  @exit_status = false
ensure
  say("\n")
  @exit_status == true if @exit_status.nil?
  if @options[:verbose]
    if @exit_status
      puts "[#{@namespace}:#{@action}] SUCCEEDED".green
    else
      puts "[#{@namespace}:#{@action}] FAILED".red
    end
    say("\n")
  end
  exit(@exit_status)
end