class Berkshelf::CookbookStore

def clean!

Destroy the contents of the initialized storage path.
def clean!
  FileUtils.rm_rf(Dir.glob("#{storage_path}/*"))
end

def cookbook(name, version)

Returns:
  • (Berkshelf::CachedCookbook, nil) -

Parameters:
  • version (String) --
  • name (String) --
def cookbook(name, version)
  path = cookbook_path(name, version)
  return nil unless path.cookbook?
  CachedCookbook.from_store_path(path)
end

def cookbook_path(name, version)

Returns:
  • (Pathname) -

Parameters:
  • version (String) --
  • name (String) --
def cookbook_path(name, version)
  storage_path.join("#{name}-#{version}")
end

def cookbooks(filter = nil)

Returns:
  • (Array) -

Parameters:
  • filter (String, Regexp) --
def cookbooks(filter = nil)
  skipped_cookbooks = []
  cookbooks = storage_path.children.collect do |path|
    begin
      Semverse::Version.split(File.basename(path).slice(CachedCookbook::DIRNAME_REGEXP, 2))
    rescue Semverse::InvalidVersionFormat
      # Skip cookbooks that were downloaded by an SCM location. These can not be considered
      # complete cookbooks.
      next
    end
    begin
      CachedCookbook.from_store_path(path)
    rescue Chef::Exceptions::MetadataNotValid
      # Skip cached cookbooks that do not have a name attribute.
      skipped_cookbooks << File.basename(path)
      next
    end
  end.compact
  if skipped_cookbooks.any?
    msg = "Skipping cookbooks #{skipped_cookbooks}. Berkshelf can only interact "
    msg << "with cookbooks which have defined the `name` attribute in their metadata.rb. If you "
    msg << "are the maintainer of any of the above cookbooks, please add the name attribute to "
    msg << "your cookbook. If you are not the maintainer, please file an issue or report the lack "
    msg << "of a name attribute as a bug.\n\n"
    msg << "You can remove each cookbook in #{skipped_cookbooks} from the Berkshelf shelf "
    msg << "by using the `berks shelf uninstall` command:\n\n"
    msg << "    $ berks shelf uninstall <name>"
    Berkshelf.log.warn msg
  end
  return cookbooks unless filter
  cookbooks.select do |cookbook|
    filter === cookbook.cookbook_name
  end
end

def default_path

Returns:
  • (String) -
def default_path
  File.join(Berkshelf.berkshelf_path, "cookbooks")
end

def import(name, version, path)

Returns:
  • (Berkshelf::CachedCookbook) -

Parameters:
  • path (String) --
  • version (String) --
  • name (String) --
def import(name, version, path)
  instance.import(name, version, path)
end

def import(name, version, path)

Returns:
  • (Berkshelf::CachedCookbook) -

Parameters:
  • path (String) --
  • version (String) --
  • name (String) --
def import(name, version, path)
  destination = cookbook_path(name, version)
  FileUtils.mv(path, destination)
  cookbook(name, version)
rescue
  FileUtils.rm_f(destination)
  raise
end

def initialize(storage_path)

Parameters:
  • storage_path (String) --
def initialize(storage_path)
  @storage_path = Pathname.new(storage_path)
  initialize_filesystem
end

def initialize_filesystem

def initialize_filesystem
  FileUtils.mkdir_p(storage_path, mode: 0755)
  unless File.writable?(storage_path)
    raise InsufficientPrivledges.new(storage_path)
  end
end

def instance

Returns:
  • (Berkshelf::CookbookStore) -
def instance
  @instance ||= new(default_path)
end

def satisfy(name, constraint)

Returns:
  • (Berkshelf::CachedCookbook, nil) -

Parameters:
  • constraint (Semverse::Constraint) --
  • name (#to_s) --
def satisfy(name, constraint)
  graph = Solve::Graph.new
  cookbooks(name).each { |cookbook| graph.artifact(name, cookbook.version) }
  name, version = Solve.it!(graph, [[name, constraint]], ENV["DEBUG_RESOLVER"] ? { ui: Berkshelf.ui } : {}).first
  cookbook(name, version)
rescue Solve::Errors::NoSolutionError
  nil
end