Class: ThinService::Command::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/thin_service/command.rb

Overview

Manages all of the available commands and handles running them.

Instance Method Summary collapse

Instance Method Details

#commandsObject

Builds a list of possible commands from the Command derivates list



358
359
360
# File 'lib/thin_service/command.rb', line 358

def commands
  ThinService::Command::COMMANDS
end

#constantize(class_name) ⇒ Object



374
375
376
377
378
379
380
# File 'lib/thin_service/command.rb', line 374

def constantize(class_name)
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ class_name
    raise NameError, "#{class_name.inspect} is not a valid constant name!"
  end

  Object.module_eval("::#{$1}", __FILE__, __LINE__)
end

Prints a list of available commands.



363
364
365
366
367
368
369
370
371
372
# File 'lib/thin_service/command.rb', line 363

def print_command_list
  puts("#{ThinService::Command::BANNER}\nAvailable commands are:\n\n")

  self.commands.each do |name|
    puts(" - #{name}\n") unless name == "start"
  end

  puts("\nEach command takes -h as an option to get help.")

end

#run(args) ⇒ Object

Runs the args against the first argument as the command name. If it has any errors it returns a false, otherwise it return true.



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/thin_service/command.rb', line 385

def run(args)
  # find the command
  cmd_name = args.shift

  if !cmd_name || cmd_name == "-?" || cmd_name == "--help"
    print_command_list
    return true
  elsif cmd_name == "--version"
    puts("ThinService #{ThinService::VERSION}")
    return true
  end

  begin
    cmd_class_name = "ThinService::Command::Commands::" + cmd_name.capitalize
    command = constantize(cmd_class_name).new( :argv => args  )
  rescue OptionParser::InvalidOption
    STDERR.puts "#$! for command '#{cmd_name}'"
    STDERR.puts "Try #{cmd_name} -h to get help."
    return false
  rescue
    STDERR.puts "ERROR RUNNING '#{cmd_name}': #$!"
    STDERR.puts "Use help command to get help"
    return false
  end

  if !command.done_validating 
    if !command.validate
      STDERR.puts "#{cmd_name} reported an error. Use thin_service #{cmd_name} -h to get help."
      return false
    else
      command.run
    end
  end

  return true
end