Class: Inspec::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/dependencies/cache.rb

Overview

Inspec::Cache 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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Cache

Returns a new instance of Cache.



19
20
21
22
23
24
# File 'lib/inspec/dependencies/cache.rb', line 19

def initialize(path = nil)
  @path = path || File.join(Inspec.config_dir, "cache")
  FileUtils.mkdir_p(@path) unless File.directory?(@path)
rescue Errno::EACCES
  # ignore
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/inspec/dependencies/cache.rb', line 18

def path
  @path
end

Instance Method Details

#archive_entry_for(key) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/inspec/dependencies/cache.rb', line 35

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

#base_path_for(cache_key) ⇒ String

Return the path to given profile in the cache.

The ‘source_url` parameter should be a URI-like string that fully specifies the source of the exact version we want to pull down.

Parameters:

  • name (String)
  • source_url (String)

Returns:

  • (String)


70
71
72
# File 'lib/inspec/dependencies/cache.rb', line 70

def base_path_for(cache_key)
  File.join(@path, cache_key)
end

#exists?(key) ⇒ Boolean

For a given name and source_url, return true if the profile exists in the Cache.

Parameters:

  • name (String)
  • source_url (String)

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/inspec/dependencies/cache.rb', line 52

def exists?(key)
  return false if key.nil? || key.empty?

  path = base_path_for(key)
  File.directory?(path) || File.exist?("#{path}.tar.gz") || File.exist?("#{path}.zip")
end

#prefered_entry_for(key) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/inspec/dependencies/cache.rb', line 26

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