Method: ComplianceEngine::ModuleLoader#initialize
- Defined in:
- lib/compliance_engine/module_loader.rb
#initialize(path, fileclass: File, dirclass: Dir) ⇒ ModuleLoader
Initialize a ModuleLoader from a Puppet module path
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 |
# File 'lib/compliance_engine/module_loader.rb', line 14 def initialize(path, fileclass: File, dirclass: Dir) raise ComplianceEngine::Error, "#{path} is not a directory" unless fileclass.directory?(path) @name = nil @version = nil @files = [] # Read the Puppet module's metadata.json = File.join(path.to_s, 'metadata.json') if fileclass.exist?() begin = ComplianceEngine::DataLoader::Json.new(, fileclass: fileclass) @name = .data['name'] @version = .data['version'] rescue => e ComplianceEngine.log.warn "Could not parse #{metadata_json}: #{e.message}" end end # In this directory, we want to look for all yaml and json files # under SIMP/compliance_profiles and simp/compliance_profiles. globs = ['SIMP/compliance_profiles', 'simp/compliance_profiles'] .select { |dir| fileclass.directory?(File.join(path, dir)) } .map { |dir| ['yaml', 'json'].map { |type| File.join(path, dir, '**', "*.#{type}") } }.flatten # Using .each here to make mocking with rspec easier. globs.each do |glob| dirclass.glob(glob).sort.each do |file| key = if Object.const_defined?(:Zip) && file.is_a?(Zip::Entry) File.join(file.zipfile.to_s, '.', file.to_s) else file.to_s end loader = if File.extname(file.to_s) == '.json' ComplianceEngine::DataLoader::Json.new(file.to_s, fileclass: fileclass, key: key) else ComplianceEngine::DataLoader::Yaml.new(file.to_s, fileclass: fileclass, key: key) end @files << loader rescue => e ComplianceEngine.log.warn "Could not load #{file}: #{e.message}" end end end |