Method: Inspec::InspecCLI#check

Defined in:
lib/inspec/cli.rb

#check(path) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/inspec/cli.rb', line 105

def check(path) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  require "inspec/resources"

  o = config
  diagnose(o)
  o["log_location"] ||= STDERR if o["format"] == "json"
  o["log_level"] ||= "warn"
  configure_logger(o)

  o[:backend] = Inspec::Backend.create(Inspec::Config.mock)
  o[:check_mode] = true
  o[:vendor_cache] = Inspec::Cache.new(o[:vendor_cache])

  # run check
  profile = Inspec::Profile.for_target(path, o)
  result = profile.check

  if o["format"] == "json"
    puts JSON.generate(result)
  else
    %w{location profile controls timestamp valid}.each do |item|
      puts format("%-12s %s", item.to_s.capitalize + ":",
                  mark_text(result[:summary][item.to_sym]))
    end
    puts

    if result[:errors].empty? && result[:warnings].empty?
      puts "No errors or warnings"
    else
      red    = "\033[31m"
      yellow = "\033[33m"
      rst    = "\033[0m"

      item_msg = lambda { |item|
        pos = [item[:file], item[:line], item[:column]].compact.join(":")
        pos.empty? ? item[:msg] : pos + ": " + item[:msg]
      }
      result[:errors].each do |item|
        puts "#{red}#{item_msg.call(item)}#{rst}"
      end
      result[:warnings].each do |item|
        puts "#{yellow}  !  #{item_msg.call(item)}#{rst}"
      end

      puts
      puts format("Summary:     %s%d errors%s, %s%d warnings%s",
                  red, result[:errors].length, rst,
                  yellow, result[:warnings].length, rst)
    end
  end
  exit 1 unless result[:summary][:valid]
rescue StandardError => e
  pretty_handle_exception(e)
end