Class: Inspec::Plugin::V2::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/plugin/v2/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Loader

Returns a new instance of Loader.



14
15
16
17
18
19
20
21
# File 'lib/inspec/plugin/v2/loader.rb', line 14

def initialize(options = {})
  @options = options
  @registry = Inspec::Plugin::V2::Registry.instance
  determine_plugin_conf_file
  read_conf_file
  unpack_conf_file
  detect_bundled_plugins unless options[:omit_bundles]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/inspec/plugin/v2/loader.rb', line 12

def options
  @options
end

#registryObject (readonly)

Returns the value of attribute registry.



12
13
14
# File 'lib/inspec/plugin/v2/loader.rb', line 12

def registry
  @registry
end

Instance Method Details

#activate(plugin_type, hook_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/inspec/plugin/v2/loader.rb', line 62

def activate(plugin_type, hook_name)
  activator = registry.find_activators(plugin_type: plugin_type, activation_name: hook_name).first
  # We want to capture literally any possible exception here, since we are storing them.
  # rubocop: disable Lint/RescueException
  begin
    impl_class = activator.activation_proc.call
    activator.activated = true
    activator.implementation_class = impl_class
  rescue Exception => ex
    activator.exception = ex
    Inspec::Log.error "Could not activate #{activator.plugin_type} hook named '#{activator.activator_name}' for plugin #{plugin_name}"
  end
  # rubocop: enable Lint/RescueException
end

#activate_mentioned_cli_plugins(cli_args = ARGV) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/inspec/plugin/v2/loader.rb', line 77

def activate_mentioned_cli_plugins(cli_args = ARGV)
  # Get a list of CLI plugin activation hooks
  registry.find_activators(plugin_type: :cli_command).each do |act|
    next if act.activated
    # If there is anything in the CLI args with the same name, activate it
    # If the word 'help' appears in the first position, load all CLI plugins
    if cli_args.include?(act.activator_name.to_s) || cli_args[0] == 'help'
      activate(:cli_command, act.activator_name)
      act.implementation_class.register_with_thor
    end
  end
end

#exit_on_load_errorObject

This should possibly be in either lib/inspec/cli.rb or Registry



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/inspec/plugin/v2/loader.rb', line 46

def exit_on_load_error
  if registry.any_load_failures?
    Inspec::Log.error 'Errors were encountered while loading plugins...'
    registry.plugin_statuses.select(&:load_exception).each do |plugin_status|
      Inspec::Log.error 'Plugin name: ' + plugin_status.name.to_s
      Inspec::Log.error 'Error: ' + plugin_status.load_exception.message
      if ARGV.include?('--debug')
        Inspec::Log.error 'Exception: ' + plugin_status.load_exception.class.name
        Inspec::Log.error 'Trace: ' + plugin_status.load_exception.backtrace.join("\n")
      end
    end
    Inspec::Log.error('Run again with --debug for a stacktrace.') unless ARGV.include?('--debug')
    exit 2
  end
end

#load_allObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/inspec/plugin/v2/loader.rb', line 23

def load_all
  registry.each do |plugin_name, plugin_details|
    # We want to capture literally any possible exception here, since we are storing them.
    # rubocop: disable Lint/RescueException
    begin
      # We could use require, but under testing, we need to repeatedly reload the same
      # plugin.
      if plugin_details.entry_point.include?('test/unit/mock/plugins')
        load plugin_details.entry_point + '.rb'
      else
        require plugin_details.entry_point
      end
      plugin_details.loaded = true
      annotate_status_after_loading(plugin_name)
    rescue ::Exception => ex
      plugin_details.load_exception = ex
      Inspec::Log.error "Could not load plugin #{plugin_name}"
    end
    # rubocop: enable Lint/RescueException
  end
end