Class: Ardb::CLI

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

Defined Under Namespace

Classes: ConnectCommand, CreateCommand, DropCommand, GenerateMigrationCommand, InvalidCommand, MigrateCommand

Constant Summary collapse

COMMANDS =
Hash.new{ |h, k| InvalidCommand.new(k) }.tap do |h|
  h['connect']            = ConnectCommand
  h['create']             = CreateCommand
  h['drop']               = DropCommand
  h['migrate']            = MigrateCommand
  h['generate-migration'] = GenerateMigrationCommand
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kernel = nil, stdout = nil, stderr = nil) ⇒ CLI

Returns a new instance of CLI.



26
27
28
29
30
# File 'lib/ardb/cli.rb', line 26

def initialize(kernel = nil, stdout = nil, stderr = nil)
  @kernel = kernel || Kernel
  @stdout = stdout || $stdout
  @stderr = stderr || $stderr
end

Class Method Details

.run(args) ⇒ Object



22
23
24
# File 'lib/ardb/cli.rb', line 22

def self.run(args)
  self.new.run(args)
end

Instance Method Details

#run(args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ardb/cli.rb', line 32

def run(args)
  begin
    $LOAD_PATH.push(Dir.pwd) unless $LOAD_PATH.include?(Dir.pwd)
    Ardb.init(false) # don't establish a connection

    cmd_name = args.shift
    cmd = COMMANDS[cmd_name].new(args)
    cmd.run
  rescue CLIRB::HelpExit
    @stdout.puts cmd.help
  rescue CLIRB::VersionExit
    @stdout.puts Ardb::VERSION
  rescue CLIRB::Error, ArgumentError, InvalidCommandError => exception
    display_debug(exception)
    @stderr.puts "#{exception.message}\n\n"
    @stdout.puts cmd.help
    @kernel.exit 1
  rescue CommandExitError
    @kernel.exit 1
  rescue StandardError => exception
    @stderr.puts "#{exception.class}: #{exception.message}"
    @stderr.puts exception.backtrace.join("\n")
    @kernel.exit 1
  end
  @kernel.exit 0
end