Module: Inspec::Reporters

Defined in:
lib/inspec/reporters.rb,
lib/inspec/reporters/cli.rb,
lib/inspec/reporters/base.rb,
lib/inspec/reporters/json.rb,
lib/inspec/reporters/yaml.rb,
lib/inspec/reporters/automate.rb,
lib/inspec/reporters/json_automate.rb

Defined Under Namespace

Classes: Automate, Base, CLI, Json, JsonAutomate, Yaml

Class Method Summary collapse

Class Method Details

.render(reporter, run_data) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



10
11
12
13
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
# File 'lib/inspec/reporters.rb', line 10

def self.render(reporter, run_data)
  name, config = reporter.dup
  config[:run_data] = run_data
  case name
  when "cli"
    reporter = Inspec::Reporters::CLI.new(config)
  when "json"
    reporter = Inspec::Reporters::Json.new(config)
  # This reporter is only used for Chef internal. We reserve the
  # right to introduce breaking changes to this reporter at any time.
  when "json-automate"
    reporter = Inspec::Reporters::JsonAutomate.new(config)
  when "automate"
    reporter = Inspec::Reporters::Automate.new(config)
  when "yaml"
    reporter = Inspec::Reporters::Yaml.new(config)
  else
    # If we made it here, it must be a plugin, and we know it exists (because we validated it in config.rb)
    activator = Inspec::Plugin::V2::Registry.instance.find_activator(plugin_type: :reporter, activator_name: name.to_sym)
    activator.activate!
    reporter = activator.implementation_class.new(config)
  end

  # optional send_report method on reporter
  return reporter.send_report if defined?(reporter.send_report)

  reporter.render
  output = reporter.rendered_output

  if config["file"]
    # create destination directory if it does not exist
    dirname = File.dirname(config["file"])
    FileUtils.mkdir_p(dirname) unless File.directory?(dirname)

    File.write(config["file"], output)
  elsif config["stdout"] == true
    print output
    $stdout.flush
  end
end

.report(reporter, run_data) ⇒ Object



51
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
# File 'lib/inspec/reporters.rb', line 51

def self.report(reporter, run_data)
  name, config = reporter.dup
  config[:run_data] = run_data
  case name
  when "json"
    reporter = Inspec::Reporters::Json.new(config)
  when "json-automate"
    reporter = Inspec::Reporters::JsonAutomate.new(config)
  when "yaml"
    reporter = Inspec::Reporters::Yaml.new(config)
  else
    # If we made it here, it might be a plugin
    begin
      activator = Inspec::Plugin::V2::Registry.instance.find_activator(plugin_type: :reporter, activator_name: name.to_sym)
      activator.activate!
      reporter = activator.implementation_class.new(config)
      unless reporter.respond_to(:report?)
        return run_data
      end
    rescue Inspec::Plugin::V2::LoadError
      # Must not have been a plugin - just return the run_data
      return run_data
    end
  end

  reporter.report
end