From 19a0a18db113199548a1632406825faf65edb025 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Wed, 10 Feb 2016 22:30:13 +0100 Subject: [PATCH] sync archive+tar+zip helpers to new dir-resolver --- lib/inspec/targets/archive.rb | 38 +++++++++++------------------------ lib/inspec/targets/tar.rb | 4 ++-- lib/inspec/targets/zip.rb | 4 ++-- 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/lib/inspec/targets/archive.rb b/lib/inspec/targets/archive.rb index 5e5607223..3f6c1733b 100644 --- a/lib/inspec/targets/archive.rb +++ b/lib/inspec/targets/archive.rb @@ -8,40 +8,26 @@ require 'inspec/targets/dir' module Inspec::Targets class ArchiveHelper < DirsResolver - def get_files(target) - files, rootdir = structure(target) + attr_reader :files + + def initialize(target, _opts) + @target = target + files, @rootdir = structure(target) # remove trailing slashes files = files.collect { |f| f.chomp('/') } # remove leading directory - files.collect { |f| - Pathname(f).relative_path_from(Pathname(rootdir)).to_s + root = Pathname(@rootdir) + @files = files.collect { |f| + Pathname(f).relative_path_from(root).to_s } end - def resolve(target, _opts = {}) - files = get_files(target) - helper = DirsHelper.get_handler(files) || - fail("Don't know how to handle folder #{target}") - - _, rootdir = structure(target) - - res = { - test: collect(helper, files, :get_filenames), - library: collect(helper, files, :get_libraries), - metadata: collect(helper, files, :get_metadata), - }.map { |as, list| - content(target, list, rootdir, base_folder: target, as: as) - } - - res.flatten - end - - # FIXME(sr) dedup inspec/targets/folder - def collect(helper, files, getter) - return [] unless helper.respond_to? getter - helper.method(getter).call(files) + def resolve(path, opts = {}) + o = (opts || {}) + o[:base_folder] = @target + content(@target, [path], @rootdir, o) end end end diff --git a/lib/inspec/targets/tar.rb b/lib/inspec/targets/tar.rb index 554be91bf..90d8818e3 100644 --- a/lib/inspec/targets/tar.rb +++ b/lib/inspec/targets/tar.rb @@ -8,7 +8,7 @@ require 'inspec/targets/archive' module Inspec::Targets class TarHelper < ArchiveHelper - def handles?(target) + def self.handles?(target) File.file?(target) && target.end_with?('.tar.gz', '.tgz') end @@ -59,5 +59,5 @@ module Inspec::Targets end end - Inspec::Targets.add_module('tar', TarHelper.new) + Inspec::Targets.add_module('tar', TarHelper) end diff --git a/lib/inspec/targets/zip.rb b/lib/inspec/targets/zip.rb index 84c5ae72e..404111f96 100644 --- a/lib/inspec/targets/zip.rb +++ b/lib/inspec/targets/zip.rb @@ -8,7 +8,7 @@ require 'inspec/targets/archive' module Inspec::Targets class ZipHelper < ArchiveHelper - def handles?(target) + def self.handles?(target) File.file?(target) and target.end_with?('.zip') end @@ -51,5 +51,5 @@ module Inspec::Targets end end - Inspec::Targets.add_module('zip', ZipHelper.new) + Inspec::Targets.add_module('zip', ZipHelper) end