Class: Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



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
# File 'lib/runner.rb', line 12

def initialize
  @defaults = { :output_format => :graphic,
                :extensions_path => File.join(File.dirname(__FILE__), 'extensions'),
                :debug => false,
                :color => true,
                :state => :run,
                :msg => nil }
  @options = CommandLine.parse!(@defaults)

  if msg = @options.delete(:message)
    puts msg
  end

  case @options.delete(:state)
  when :exit
    exit 0
  when :fail
    exit 1
  end

  if @options[:debug]
    Log.initialize
  end

  unless @options[:color]
    Rainbow.enabled = false
  end

  Log.debug { "Starting policy check with configured option - #{@options.inspect}" }

  @extensions = Extensions.new(@options[:extensions_path])
  policy_file = CommandLine.policy!
  @policies = load_policies(policy_file).map { |policy| Policy.new(policy) }
  @exit_code = 0
end

Instance Attribute Details

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



10
11
12
# File 'lib/runner.rb', line 10

def exit_code
  @exit_code
end

Instance Method Details

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/runner.rb', line 48

def run
  processed_policies = []

  @policies.each do |policy|
    if policy.enabled?
      processed_policies << policy.check_rules
    end
  end

  result_viewer = ResultViewer.new(processed_policies)

  if result_viewer.run_state[:successes] != result_viewer.run_state[:total]
    @exit_code = 1
  end

  case @options[:output_format]
  when :graphic
    result_viewer.to_s
  when :json
    result_viewer.to_json
  when :yaml
    result_viewer.to_yaml
  # TODO(ploubser): Fix this
  #when :csv
  #  result_viewer.to_csv
  end
end