move to symbols-based fields in profile params

This commit is contained in:
Dominik Richter 2015-10-26 22:57:34 +01:00
parent 7779cdb469
commit 32e5e3ec29
3 changed files with 29 additions and 27 deletions

View file

@ -27,17 +27,17 @@ module Inspec
description
version
}.each do |name|
define_method name do |arg|
params[name] = arg
define_method name.to_sym do |arg|
params[name.to_sym] = arg
end
end
def supports(sth, version = nil)
params['supports'] ||= []
params['supports'].push(
params[:supports] ||= []
params[:supports].push(
{
'os' => sth,
'version' => version,
os: sth,
version: version,
},
)
end
@ -45,20 +45,20 @@ module Inspec
def valid?
is_valid = true
%w{ name title version summary }.each do |field|
next unless params[field].nil?
@log.error("Missing profile #{field} in metadata.rb")
next unless params[field.to_sym].nil?
@logger.error("Missing profile #{field} in metadata.rb")
is_valid = false
end
%w{ maintainer copyright }.each do |field|
next unless params[field].nil?
@log.warn("Missing profile #{field} in metadata.rb")
next unless params[field.to_sym].nil?
@logger.warn("Missing profile #{field} in metadata.rb")
is_valid = false
end
is_valid && @missing_methods.empty?
end
def method_missing(sth, *args)
@log.warn "metadata.rb doesn't support: #{sth} #{args}"
@logger.warn "metadata.rb doesn't support: #{sth} #{args}"
@missing_methods.push(sth)
end
@ -72,7 +72,7 @@ module Inspec
res = Metadata.new(logger)
res.instance_eval(File.read(path), path, 1)
res.dict['name'] = profile_id.to_s unless profile_id.to_s.empty?
res.params[:name] = profile_id.to_s unless profile_id.to_s.empty?
res
end
end

View file

@ -8,7 +8,8 @@ require 'inspec/metadata'
module Inspec
class Profile
def self.from_path(path, options = nil)
opt = options.dup || {}
opt = {}
options.each { |k, v| opt[k.to_sym] = v } unless options.nil?
opt[:path] = path
Profile.new(opt)
end
@ -18,9 +19,9 @@ module Inspec
def initialize(options = nil)
@options = options || {}
@profile_id = options[:id] || nil
@params = {}
@logger = options.delete(:logger) || Logger.new(nil)
@profile_id = options.delete(:profile_id)
@logger = options[:logger] || Logger.new(nil)
@path = @options[:path]
fail 'Cannot read an empty path.' if @path.nil? || @path.empty?
@ -29,7 +30,7 @@ module Inspec
@metadata = read_metadata
@params = @metadata.params unless @metadata.nil?
@params['rules'] = rules = {}
@params[:rules] = rules = {}
@runner = Runner.new(
id: @profile_id,
backend: :mock,
@ -49,7 +50,7 @@ module Inspec
def info
res = @params.dup
rules = {}
res['rules'].each do |id, rule|
res[:rules].each do |id, rule|
next if id.to_s.empty?
data = rule.dup
@ -59,7 +60,7 @@ module Inspec
data[:impact] = 0.0 if data[:impact] < 0.0
rules[id] = data
end
res['rules'] = rules
res[:rules] = rules
res
end
@ -81,26 +82,26 @@ module Inspec
@logger.info "Checking profile in #{@path}"
if @params['name'].to_s.empty?
if @params[:name].to_s.empty?
error.call('No profile name defined')
elsif !(@params['name'].to_s =~ %r{^\S+\/\S+$})
elsif !(@params[:name].to_s =~ %r{^\S+\/\S+$})
error.call('Profile name must be defined as: OWNER/ID')
end
warn.call('No version defined') if @params['version'].to_s.empty?
warn.call('No title defined') if @params['title'].to_s.empty?
warn.call('No maintainer defined') if @params['maintainer'].to_s.empty?
warn.call('No supports defined') if @params['supports'].empty?
warn.call('No version defined') if @params[:name].to_s.empty?
warn.call('No title defined') if @params[:name].to_s.empty?
warn.call('No maintainer defined') if @params[:name].to_s.empty?
warn.call('No supports defined') if @params[:name].empty?
@logger.info 'Metadata OK.' if no_warnings
no_warnings = true
if @params['rules'].empty?
if @params[:name].empty?
warn.call('No rules were found.')
else
@logger.debug "Found #{@params['rules'].length} rules."
@logger.debug "Found #{@params[:name].length} rules."
end
@params['rules'].each do |id, rule|
@params[:name].each do |id, rule|
error.call('Avoid rules with empty IDs') if id.nil? or id.empty?
warn.call("Rule #{id} has no title") if rule[:title].to_s.empty?
warn.call("Rule #{id} has no description") if rule[:desc].to_s.empty?

View file

@ -15,6 +15,7 @@ end
require 'inspec/resource'
require 'inspec/backend'
require 'inspec/profile'
class MockLoader
# pass the os identifier to emulate a specific operating system