Class: Fetchers::Local
- Inherits:
-
Object
- Object
- Fetchers::Local
- Defined in:
- lib/fetchers/local.rb
Class Method Summary collapse
Instance Method Summary collapse
- #archive_path ⇒ Object
- #cache_key ⇒ Object
- #fetch(path) ⇒ Object
-
#initialize(target, opts = {}) ⇒ Local
constructor
A new instance of Local.
- #perform_shasum(target) ⇒ Object
- #resolved_source ⇒ Object
- #sha256 ⇒ Object
- #writable? ⇒ Boolean
Constructor Details
#initialize(target, opts = {}) ⇒ Local
Returns a new instance of Local.
44 45 46 47 48 |
# File 'lib/fetchers/local.rb', line 44 def initialize(target, opts = {}) @target = target @backend = opts[:backend] @archive_shasum = nil end |
Class Method Details
.resolve(target) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/fetchers/local.rb', line 12 def self.resolve(target) if target.is_a?(String) local_path = resolve_from_string(target) new(local_path) if local_path elsif target.is_a?(Hash) local_path = resolve_from_hash(target) new(local_path, target) if local_path end end |
.resolve_from_hash(target) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/fetchers/local.rb', line 22 def self.resolve_from_hash(target) return unless target.key?(:path) if target.key?(:cwd) File.(target[:path], target[:cwd]) else target[:path] end end |
.resolve_from_string(target) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/fetchers/local.rb', line 32 def self.resolve_from_string(target) # Support "urls" in the form of file:// if target.start_with?('file://') target = target.gsub(%r{^file://}, '') else # support for windows paths target = target.tr('\\', '/') end target if File.exist?(File.(target)) end |
Instance Method Details
#archive_path ⇒ Object
85 86 87 |
# File 'lib/fetchers/local.rb', line 85 def archive_path @target end |
#cache_key ⇒ Object
93 94 95 |
# File 'lib/fetchers/local.rb', line 93 def cache_key sha256.to_s end |
#fetch(path) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/fetchers/local.rb', line 50 def fetch(path) # If `inspec exec` is used then we should not vendor/fetch. This makes # local development easier and more predictable. return @target if Inspec::BaseCLI.inspec_cli_command == :exec # Skip vendoring if @backend is not set (example: ad hoc runners) return @target unless @backend if File.directory?(@target) # Create an archive, checksum, and move to the vendor directory Dir.mktmpdir do |tmpdir| temp_archive = File.join(tmpdir, "#{File.basename(@target)}.tar.gz") opts = { backend: @backend, output: temp_archive, } # Create a temporary archive at `opts[:output]` Inspec::Profile.for_target(@target, opts).archive(opts) checksum = perform_shasum(temp_archive) final_path = File.join(path, checksum) FileUtils.mkdir_p(final_path) Inspec::FileProvider.for_path(temp_archive).extract(final_path) end else # Verify profile (archive) is valid and extract to vendor directory opts = { backend: @backend } Inspec::Profile.for_target(@target, opts).check Inspec::FileProvider.for_path(@target).extract(path) end @target end |
#perform_shasum(target) ⇒ Object
107 108 109 |
# File 'lib/fetchers/local.rb', line 107 def perform_shasum(target) @archive_shasum ||= OpenSSL::Digest::SHA256.digest(File.read(target)).unpack('H*')[0] end |
#resolved_source ⇒ Object
111 112 113 114 115 |
# File 'lib/fetchers/local.rb', line 111 def resolved_source h = { path: @target } h[:sha256] = sha256 if sha256 h end |
#sha256 ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/fetchers/local.rb', line 97 def sha256 if !@archive_shasum.nil? @archive_shasum elsif File.directory?(@target) nil else perform_shasum(@target) end end |
#writable? ⇒ Boolean
89 90 91 |
# File 'lib/fetchers/local.rb', line 89 def writable? File.directory?(@target) end |