Puppet Function: complyadm::checks::format_results

Defined in:
lib/puppet/functions/complyadm/checks/format_results.rb
Function type:
Ruby 4.x API

Overview

complyadm::checks::format_results(String $header, Hash $results)String

Formats check results for display in the console

Parameters:

  • header (String)

    the text displayed at the top of the section in white text

  • result (Hash)

    hash containing the check results to display

  • results (Hash)

Returns:

  • (String)


2
3
4
5
6
7
8
9
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppet/functions/complyadm/checks/format_results.rb', line 2

Puppet::Functions.create_function(:'complyadm::checks::format_results') do
  # Formats check results for display in the console
  # @param [String] header the text displayed at the top of the section in white text
  # @param [Hash] result hash containing the check results to display
  # @returns [String] gorgeous formatted output, complete with colors
  dispatch :format_results do
    param 'String', :header
    param 'Hash', :results
    return_type 'String'
  end

  def format_results(header, results)
    message = "#{header}\n"
    message += format_passed(results['passed'])
    message += format_failed(results['failed'])
    message + "\n"
  end

  def format_passed(passed)
    message = ''
    if !passed.empty?
      message += format_checks(passed, green_passed)
    end
    message
  end

  def format_failed(failed)
    message = ''
    if !failed.empty?
      message += format_checks(failed, red_failed)
    end
    message
  end

  def format_checks(section, prefix)
    formatted_section = ''
    section.each do |item|
      formatted_section += "  #{prefix} #{item}#{end_color}\n"
    end
    formatted_section
  end

  def green_passed
    "\e[32m\u2713 [PASSED]"
  end

  def red_failed
    "\e[31m\u2715 [FAILED]"
  end

  def end_color
    "\e[0m"
  end

  def print_number_of_checks(number)
    "#{number} #{pluralize_check(number)}"
  end

  def pluralize_check(number)
    (number == 1) ? 'check' : 'checks'
  end
end