Sha256: ff6863ab1d2926c57a6b2e1c2c11593a54403805441f34232c4ca54437ab3eac

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

# encoding: utf-8
require 'digest'
require 'fileutils'

module Inspec
  #
  # VendorIndex manages an on-disk cache of inspec profiles.  The
  # cache can contain:
  #
  #  - .tar.gz profile archives
  #  - .zip profile archives
  #  - unpacked profiles
  #
  # Cache entries names include a hash of their source to prevent
  # conflicts between depenedencies with the same name from different
  # sources.
  #
  #
  class VendorIndex
    attr_reader :path
    def initialize(path = nil)
      @path = path || File.join(Dir.home, '.inspec', 'cache')
      FileUtils.mkdir_p(@path) unless File.directory?(@path)
    end

    def prefered_entry_for(key)
      path = base_path_for(key)
      if File.directory?(path)
        path
      else
        archive_entry_for(key)
      end
    end

    def archive_entry_for(key)
      path = base_path_for(key)
      if File.exist?("#{path}.tar.gz")
        "#{path}.tar.gz"
      elsif File.exist?("#{path}.zip")
        "#{path}.zip"
      end
    end

    #
    # For a given name and source_url, return true if the
    # profile exists in the VendorIndex.
    #
    # @param [String] name
    # @param [String] source_url
    # @return [Boolean]
    #
    def exists?(key)
      path = base_path_for(key)
      File.directory?(path) || File.exist?("#{path}.tar.gz") || File.exist?("#{path}.zip")
    end

    #
    # Return the path to given profile in the vendor index.
    #
    # The `source_url` parameter should be a URI-like string that
    # fully specifies the source of the exact version we want to pull
    # down.
    #
    # @param [String] name
    # @param [String] source_url
    # @return [String]
    #
    def base_path_for(cache_key)
      File.join(@path, cache_key)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
inspec-0.35.0 lib/inspec/dependencies/vendor_index.rb
inspec-0.34.1 lib/inspec/dependencies/vendor_index.rb
inspec-0.34.0 lib/inspec/dependencies/vendor_index.rb