Class: CMSScanner::Controllers

Inherits:
Array
  • Object
show all
Defined in:
lib/cms_scanner/controllers.rb

Overview

Controllers Container

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_parser = OptParseValidator::OptParser.new(nil, 40)) ⇒ Controllers

Returns a new instance of Controllers.

Parameters:

  • options_parser (OptParsevalidator::OptParser)


9
10
11
12
13
14
15
# File 'lib/cms_scanner/controllers.rb', line 9

def initialize(option_parser = OptParseValidator::OptParser.new(nil, 40))
  @option_parser = option_parser

  register_config_files

  option_parser.config_files.result_key = 'cli_options'
end

Instance Attribute Details

#option_parserObject (readonly)

Returns the value of attribute option_parser.



6
7
8
# File 'lib/cms_scanner/controllers.rb', line 6

def option_parser
  @option_parser
end

#runningObject (readonly)

Returns the value of attribute running.



6
7
8
# File 'lib/cms_scanner/controllers.rb', line 6

def running
  @running
end

Instance Method Details

#<<(controller) ⇒ Object

Parameters:



29
30
31
32
33
34
35
36
37
# File 'lib/cms_scanner/controllers.rb', line 29

def <<(controller)
  options = controller.cli_options

  unless include?(controller)
    option_parser.add(*options) if options
    super(controller)
  end
  self
end

#register_config_filesObject

Adds the potential option file paths to the option_parser



18
19
20
21
22
23
24
# File 'lib/cms_scanner/controllers.rb', line 18

def register_config_files
  [Dir.home, Dir.pwd].each do |dir|
    option_parser.config_files.class.supported_extensions.each do |ext|
      option_parser.config_files << Pathname.new(dir).join(".#{NS.app_name}", "scan.#{ext}").to_s
    end
  end
end

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cms_scanner/controllers.rb', line 39

def run
  NS::ParsedCli.options     = option_parser.results
  first.class.option_parser = option_parser # To be able to output the help when -h/--hh

  redirect_output_to_file(NS::ParsedCli.output) if NS::ParsedCli.output

  Timeout.timeout(NS::ParsedCli.max_scan_duration, NS::Error::MaxScanDurationReached) do
    each(&:before_scan)

    @running = true

    each(&:run)
  end
ensure
  # The rescue is there to prevent unfinished requests to raise an error, which would prevent
  # the reverse_each to run
  # rubocop:disable Style/RescueModifier
  NS::Browser.instance.hydra.abort rescue nil
  # rubocop:enable Style/RescueModifier

  # Reverse is used here as the app/controllers/core#after_scan finishes the output
  # and must be the last one to be executed. It also guarantee that stats will be output
  # even when an error occurs, which could help in debugging.
  # However, the #after_scan methods are only executed if the scan was running, and won't be
  # called when there is a CLI error, or just -h/--hh/--version for example
  reverse_each(&:after_scan) if running
end