Class: Abide::CLI::CemGenerateCoverageReport

Inherits:
AbideCommand
  • Object
show all
Defined in:
lib/abide_dev_utils/cli/cem.rb

Constant Summary collapse

CMD_NAME =
'coverage-report'
CMD_SHORT =
'Generates control coverage report'
CMD_LONG =
<<-EOLC.chomp
Generates report of resources that are associated with controls in mapping data. This command must
be run from a module directory.
EOLC

Constants included from AbideDevUtils::Config

AbideDevUtils::Config::DEFAULT_PATH

Instance Method Summary collapse

Methods included from AbideDevUtils::Config

config_section, #config_section, fetch, #fetch, to_h, #to_h

Constructor Details

#initializeCemGenerateCoverageReport

Returns a new instance of CemGenerateCoverageReport.



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

def initialize
  super(CMD_NAME, CMD_SHORT, CMD_LONG, takes_commands: false)
  options.on('-o [FILE]', '--out-file [FILE]', 'Path to save the coverage report') { |f| @data[:file] = f }
  options.on('-f [FORMAT]', '--format [FORMAT]', 'The format to output the report in (hash, json, yaml)') do |f|
    @data[:format] = f
  end
  options.on('-B [BENCHMARK]', '--benchmark [BENCHMARK]', 'Specify the benchmark to show coverage for') do |x|
    @data[:benchmark] = x
  end
  options.on('-P [PROFILE]', '--profile [PROFILE]', 'Specifiy the profile to show coverage for') do |x|
    @data[:profile] = x
  end
  options.on('-L [LEVEL]', '--level [LEVEL]', 'Specify the level to show coverage for') do |l|
    @data[:profile] = l
  end
  options.on('-I', '--ignore-benchmark-errors', 'Ignores errors while generating benchmark reports') do
    @data[:ignore_all] = true
  end
  options.on('-X [XCCDF_DIR]', '--xccdf-dir [XCCDF_DIR]', 'If specified, the coverage report will be correlated with info from the benchmark XCCDF files') do |d|
    @data[:xccdf_dir] = d
  end
  options.on('-v', '--verbose', 'Will output the report to the console') { @data[:verbose] = true }
  options.on('-q', '--quiet', 'Will not output anything to the console') { @data[:quiet] = true }
end

Instance Method Details

#executeObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/abide_dev_utils/cli/cem.rb', line 66

def execute
  file_name = @data.fetch(:file, 'coverage_report')
  out_format = @data.fetch(:format, 'yaml')
  quiet = @data.fetch(:quiet, false)
  console = @data.fetch(:verbose, false) && !quiet
  generate_opts = {
    benchmark: @data.fetch(:benchmark),
    profile: @data.fetch(:profile),
    level: @data.fetch(:level),
    ignore_benchmark_errors: @data.fetch(:ignore_all, false),
    xccdf_dir: @data.fetch(:xccdf_dir),
  }
  AbideDevUtils::Output.simple('Generating coverage report...') unless quiet
  coverage = AbideDevUtils::CEM::Generate::CoverageReport.generate(format_func: :to_h, opts: generate_opts)
  AbideDevUtils::Output.simple("Saving coverage report to #{file_name}...")
  case out_format
  when /yaml/i
    AbideDevUtils::Output.yaml(coverage, console: console, file: file_name)
  when /json/i
    AbideDevUtils::Output.json(coverage, console: console, file: file_name)
  else
    File.open(file_name, 'w') do |f|
      AbideDevUtils::Output.simple(coverage.to_s, stream: f)
    end
  end
end