Class: Bookbinder::Cli

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

Instance Method Summary collapse

Constructor Details

#initialize(version_control_system) ⇒ Cli

Returns a new instance of Cli.



10
11
12
# File 'lib/bookbinder/cli.rb', line 10

def initialize(version_control_system)
  @version_control_system = version_control_system
end

Instance Method Details

#run(args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bookbinder/cli.rb', line 14

def run(args)
  command_name, *command_arguments = args

  logger = DeprecatedLogger.new
  commands = Commands::Collection.new(
    logger,
    colorized_streams,
    version_control_system
  )

  command_validator = CommandValidator.new(commands, commands.help.usage_message)
  command_runner = CommandRunner.new(logger, commands)
  command_name = command_name ? command_name : '--help'

  colorizer = Colorizer.new
  terminal = Terminal.new(colorizer)

  user_message = command_validator.validate(command_name)
  terminal.update(user_message)

  if user_message.error?
    return 1
  end

  begin
    command_runner.run command_name, command_arguments

  rescue Config::CfCredentials::CredentialKeyError => e
    colorized_streams[:err].puts "#{e.message}, in credentials.yml"
    1
  rescue KeyError => e
    colorized_streams[:err].puts "#{e.message} from your configuration."
    1
  rescue CliError::UnknownCommand => e
    colorized_streams[:out].puts e.message
    1
  rescue RuntimeError => e
    colorized_streams[:err].puts e.message
    1
  end
end