Sha256: c4bf363f8cdd397763bcb6ef626c783e8f4e0cedb80d1c13bb968fbcb038e710

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

# 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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
inspec-2.1.81 lib/fetchers/local.rb
inspec-2.1.21 lib/fetchers/local.rb
inspec-2.1.10 lib/fetchers/local.rb
inspec-2.0.32 lib/fetchers/local.rb
inspec-2.0.17 lib/fetchers/local.rb
inspec-1.51.15 lib/fetchers/local.rb