Class: Ardb::CLI

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

Defined Under Namespace

Modules: MigrateCommandBehaviors, MigrateDirectionBehaviors, MigrateStepDirectionBehaviors, MigrateStyleBehaviors, ValidCommand Classes: CLIRB, CommandSet, ConnectCommand, CreateCommand, DropCommand, GenerateMigrationCommand, InvalidCommand, MigrateBackwardCommand, MigrateCommand, MigrateDownCommand, MigrateForwardCommand, MigrateUpCommand

Constant Summary collapse

COMMANDS =
CommandSet.new{ |unknown|
  InvalidCommand.new(unknown)
}.tap do |c|
  c.add(ConnectCommand)
  c.add(CreateCommand)
  c.add(DropCommand)
  c.add(GenerateMigrationCommand)
  c.add(MigrateCommand)
  c.add(MigrateUpCommand)
  c.add(MigrateDownCommand)
  c.add(MigrateForwardCommand)
  c.add(MigrateBackwardCommand)
end
InvalidCommandError =
Class.new(ArgumentError)
CommandExitError =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CLI.



28
29
30
31
32
# File 'lib/ardb/cli.rb', line 28

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

Class Method Details

.run(args) ⇒ Object



24
25
26
# File 'lib/ardb/cli.rb', line 24

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

Instance Method Details

#run(args) ⇒ Object



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

def run(args)
  begin
    $LOAD_PATH.push(Dir.pwd) unless $LOAD_PATH.include?(Dir.pwd)

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