Class: MOSAIK::CLI

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

Overview

Command line interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mosaik/cli.rb', line 13

def initialize(args)
  @options = {
    directory: Dir.pwd,
    debug: false,
  }

  @args = args
  @command_args = []

  parse
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



11
12
13
# File 'lib/mosaik/cli.rb', line 11

def args
  @args
end

#command_argsObject (readonly)

Returns the value of attribute command_args.



11
12
13
# File 'lib/mosaik/cli.rb', line 11

def command_args
  @command_args
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/mosaik/cli.rb', line 11

def options
  @options
end

Instance Method Details

#callObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mosaik/cli.rb', line 52

def call
  # Extract command name
  command_name = command_args.shift

  raise UsageError, "no command specified" unless command_name

  klass = "MOSAIK::Commands::#{command_name.camelize}".safe_constantize

  raise UsageError, "unknown command: #{command_name}" unless klass

  # Add command arguments to global argument parser (for the usage message)
  klass.arguments.each do |args, kwargs, block|
    parser.on(*args, **kwargs, &block)
  end
  parser.separator("\n")

  # Execute command
  command = klass
    .new(options, *command_args)

  command
    .validate

  command
    .call
rescue UsageError => e
  # Don't print tail if no message was passed
  return usage if e.message == e.class.name

  usage(tail: "#{File.basename($PROGRAM_NAME)}: #{e.message}")
rescue Error => e
  fatal "#{File.basename($PROGRAM_NAME)}: #{e.message}"

  raise ExitError, 1
end

#parseObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mosaik/cli.rb', line 25

def parse
  # Parse command line arguments (in order) and extract non-option arguments
  # (unrecognized option values). Raise for invalid option arguments (unrecognized
  # option keys). "--foo FOO --bar BAR" will result in "--foo" and "FOO" being parsed
  # correctly, "--bar" and "BAR" will be extracted.
  # This needs to be in a separate method due to the retry logic.
  parser.order!(args, into: options) { |value| @command_args << value }
rescue OptionParser::InvalidOption => e
  @command_args += e.args
  retry
end

#validateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mosaik/cli.rb', line 37

def validate
  raise OptionError, "invalid directory: #{options[:directory]}" unless File.exist?(options[:directory])
  raise OptionError, "not a directory: #{options[:directory]}" unless File.directory?(options[:directory])

  # Set log level
  MOSAIK.logger.level = options[:debug] ? "debug" : "info"

  # Set configuration
  MOSAIK.configuration = Configuration.from(File.join(options[:directory], "mosaik.yml"))
rescue Error => e
  fatal e.message

  raise ExitError, 1
end