chain fetchers together

This commit is contained in:
Dominik Richter 2016-02-21 10:25:00 +01:00 committed by Stephan Renatus
parent 7b073fe153
commit d293550375
5 changed files with 17 additions and 12 deletions

View file

@ -13,7 +13,6 @@ module Fetchers
attr_reader :files
def self.resolve(target)
target = target.path if target.respond_to?(:path)
return nil unless File.file?(target)
return nil unless target.end_with?('.tar.gz', '.tgz')
new(target)

View file

@ -20,7 +20,9 @@ module Fetchers
target = transform(target)
# TODO: for now, this can be much less strict now vv
return nil unless target.end_with?('tar.gz', 'zip')
resolve_url(target, opts)
# fetch this url and hand it off
res = new(target, opts)
resolve_next(res.archive.path, res)
rescue URI::Error => _e
nil
end
@ -90,13 +92,11 @@ module Fetchers
archive
end
def self.resolve_url(url, opts)
archive = download_archive(url, opts)
# TODO: At the moment we hand over the Tempfile object. This is necessary,
# since otherwise Ruby will delete the file on disk(!!) as soon as
# this call is finished (due to garbage-collection of Tempfile).
# Resolve this by handling over the intermediate resolver to the next.
resolve_next(archive)
attr_reader :archive
def initialize(url, opts)
@target = url
@archive = self.class.download_archive(url, opts)
end
end
end

View file

@ -12,7 +12,6 @@ module Fetchers
attr_reader :files
def self.resolve(target)
target = target.path if target.respond_to?(:path)
return nil unless File.file?(target) and target.end_with?('.zip')
new(target)
end

View file

@ -39,11 +39,14 @@ module Inspec
#
# @param [Any] the current target that needs resolving
# @return [Fetcher] a fetcher if it can be resolved, nil otherwise
def self.resolve_next(target)
Inspec::Fetcher.resolve(target)
def self.resolve_next(target, parent)
res = Inspec::Fetcher.resolve(target)
res.parent = parent
res
end
attr_reader :target
attr_accessor :parent
# Provide a list of files that are available to this fetcher.
#

View file

@ -27,6 +27,10 @@ describe Fetchers::Url do
_(res).must_be_kind_of Fetchers::Tar
end
it 'must be resolved to the final format' do
_(res.parent).must_be_kind_of fetcher
end
it 'must contain all files' do
_(res.files).must_equal ["inspec.yml", "controls", "controls/filesystem_spec.rb"]
end