Class: Gerrit::Cli::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/gerrit/cli/dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ Dispatcher

Returns a new instance of Dispatcher.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gerrit/cli/dispatcher.rb', line 14

def initialize(logger=nil)
  @logger = logger ||  Logger.new(STDOUT)
  @logger.level = Logger::INFO

  runner = Gerrit::Cli::ShellRunner.new(logger)

  @commands = {
    'clone' => Gerrit::Cli::Command::Clone.new(logger, runner),
    'push'  => Gerrit::Cli::Command::Push.new(logger, runner),
  }

  @commands['help'] = Gerrit::Cli::Command::Help.new(logger, @commands)
end

Instance Method Details

#run_command(argv) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gerrit/cli/dispatcher.rb', line 28

def run_command(argv)
  if argv.empty?
    @logger.info("Available Commands:")
    @logger.info(@commands['help'].commands_summary)
  else
    args = argv.dup
    command_name = args.shift
    if command = @commands[command_name]
      command.run(args)
    else
      @logger.error("ERROR: Unknown command '#{command_name}'")
      @commands['help'].show_command_summaries
    end
  end
rescue Gerrit::Cli::UsageError => ue
  @logger.error("ERROR: #{ue}\n")
  command.show_usage
  exit 1
rescue => e
  @logger.error("ERROR: #{e}")
  @logger.debug(e.backtrace.join("\n")) if e.backtrace
  exit 1
end