Class: Licensed::Commands::Command

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

Direct Known Subclasses

Cache, Environment, List, Notices, Status

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Command

Returns a new instance of Command.



9
10
11
# File 'lib/licensed/commands/command.rb', line 9

def initialize(config:)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/licensed/commands/command.rb', line 5

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/licensed/commands/command.rb', line 7

def options
  @options
end

#reporterObject (readonly)

Returns the value of attribute reporter.



6
7
8
# File 'lib/licensed/commands/command.rb', line 6

def reporter
  @reporter
end

Instance Method Details

#create_reporter(options) ⇒ Object

Creates a reporter to use during a command run

options - The options the command was run with

Returns the reporter to use during the command run



34
35
36
37
38
39
40
41
42
43
# File 'lib/licensed/commands/command.rb', line 34

def create_reporter(options)
  return options[:reporter] if options[:reporter].is_a?(Licensed::Reporters::Reporter)

  if options[:reporter].is_a?(String)
    klass = "#{options[:reporter].capitalize}Reporter"
    return Licensed::Reporters.const_get(klass).new if Licensed::Reporters.const_defined?(klass)
  end

  default_reporter(options)
end

#default_reporter(options) ⇒ Object

Returns the default reporter to use during the command run

options - The options the command was run with

Raises an error



50
51
52
# File 'lib/licensed/commands/command.rb', line 50

def default_reporter(options)
  raise "`default_reporter` must be implemented by commands"
end

#run(**options) ⇒ Object

Run the command

options - Options to run the command with

Returns whether the command was a success



18
19
20
21
22
23
24
25
26
27
# File 'lib/licensed/commands/command.rb', line 18

def run(**options)
  @options = options
  @reporter = create_reporter(options)

  command_report = Licensed::Report.new(name: nil, target: self)
  run_command(command_report)
ensure
  @options = nil
  @reporter = nil
end