inspec/lib/fetchers/local.rb
Adam Leff 4b9acb4800 Bump Rubocop to 0.49.1 (#2323)
* Bump Rubocop to 0.49.1

This change bumps Rubocop to 0.49.1. There have been a lot of changes
since 0.39.0 and this PR is hopefully a nice compromise of turning off
certain cops and updating our codebase to take advantage of new Ruby
2.3 methods and operators.

Signed-off-by: Adam Leff <adam@leff.co>

* Set end-of-line format to line-feed only, avoid Windows-related CRLF issues

Signed-off-by: Adam Leff <adam@leff.co>
2017-11-21 08:49:41 +01:00

74 lines
1.5 KiB
Ruby

# encoding: utf-8
# author: Dominik Richter
# author: Christoph Hartmann
require 'openssl'
module Fetchers
class Local < Inspec.fetcher(1)
name 'local'
priority 0
def self.resolve(target)
local_path = if target.is_a?(String)
resolve_from_string(target)
elsif target.is_a?(Hash)
resolve_from_hash(target)
end
new(local_path) if local_path
end
def self.resolve_from_hash(target)
return unless target.key?(:path)
local_path = target[:path]
local_path = File.expand_path(local_path, target[:cwd]) if target.key?(:cwd)
local_path
end
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?(target)
end
def initialize(target)
@target = target
end
def fetch(_path)
archive_path
end
def archive_path
@target
end
def writable?
File.directory?(@target)
end
def cache_key
sha256.to_s
end
def sha256
return nil if File.directory?(@target)
@archive_shasum ||=
OpenSSL::Digest::SHA256.digest(File.read(@target)).unpack('H*')[0]
end
def resolved_source
h = { path: @target }
h[:sha256] = sha256 if sha256
h
end
end
end