2016-02-21 01:02:39 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
|
|
|
|
module Fetchers
|
|
|
|
class Local < Inspec.fetcher(1)
|
|
|
|
name 'local'
|
|
|
|
priority 0
|
|
|
|
|
|
|
|
attr_reader :files
|
|
|
|
|
|
|
|
def self.resolve(target)
|
2016-08-19 14:21:04 +00:00
|
|
|
return nil unless target.is_a?(String)
|
|
|
|
|
|
|
|
# Support "urls" in the form of file://
|
|
|
|
if target.start_with?('file://')
|
|
|
|
target = target.gsub(%r{^file://}, '')
|
2016-06-30 03:18:30 +00:00
|
|
|
else
|
|
|
|
# support for windows paths
|
2016-08-24 14:06:12 +00:00
|
|
|
target = target.tr('\\', '/')
|
2016-08-19 14:21:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if !File.exist?(target)
|
2016-08-17 13:25:07 +00:00
|
|
|
nil
|
|
|
|
else
|
|
|
|
new(target)
|
|
|
|
end
|
2016-02-21 01:02:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(target)
|
2016-08-19 14:21:04 +00:00
|
|
|
@target = target
|
2016-02-21 01:02:39 +00:00
|
|
|
if File.file?(target)
|
|
|
|
@files = [target]
|
|
|
|
else
|
|
|
|
@files = Dir[File.join(target, '**', '*')]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(file)
|
|
|
|
return nil unless files.include?(file)
|
|
|
|
return nil unless File.file?(file)
|
|
|
|
File.read(file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|