class ChefCLI::Policyfile::ChefRepoCookbookSource

Attributes

path[R]

path to a chef-repo or the cookbook path under it

preferred_cookbooks[R]
ui[RW]

UI object for output

Public Class Methods

new(path) { |self| ... } click to toggle source

Constructor

@param path [String] path to a chef-repo or the cookbook path under it

# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 37
def initialize(path)
  self.path = path
  @ui = UI.new
  @preferred_cookbooks = []
  yield self if block_given?
end

Public Instance Methods

==(other) click to toggle source
# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 56
def ==(other)
  other.is_a?(self.class) && other.path == path && other.preferred_cookbooks == preferred_cookbooks
end
default_source_args() click to toggle source
# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 44
def default_source_args
  [:chef_repo, path]
end
desc() click to toggle source
# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 80
def desc
  "chef_repo(#{path})"
end
null?() click to toggle source
# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 76
def null?
  false
end
preferred_for(*cookbook_names) click to toggle source
# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 48
def preferred_for(*cookbook_names)
  preferred_cookbooks.concat(cookbook_names)
end
preferred_source_for?(cookbook_name) click to toggle source
# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 52
def preferred_source_for?(cookbook_name)
  preferred_cookbooks.include?(cookbook_name)
end
source_options_for(cookbook_name, cookbook_version) click to toggle source

Returns the metadata (path and version) for an individual cookbook

@return [Hash] metadata for a single cookbook version

# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 72
def source_options_for(cookbook_name, cookbook_version)
  { path: cookbook_version_paths[cookbook_name][cookbook_version], version: cookbook_version }
end
universe_graph() click to toggle source

Calls the slurp_metadata! helper once to calculate the @universe_graph and @cookbook_version_paths metadata. Returns the @universe_graph.

@return [Hash] universe_graph

# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 64
def universe_graph
  slurp_metadata! if @universe_graph.nil?
  @universe_graph
end

Private Instance Methods

cookbook_repo() click to toggle source

@return [Chef::CookbookLoader] cookbook loader using on disk FileVendor

# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 129
def cookbook_repo
  @cookbook_repo ||= begin
    Chef::Cookbook::FileVendor.fetch_from_disk(path)
    Chef::CookbookLoader.new(path)
  end
end
cookbook_version_paths() click to toggle source

Calls the slurp_metadata! helper once to calculate the @universe_graph and @cookbook_version_paths metadata. Returns the @cookbook_version_paths.

@return [Hash] cookbook_version_paths

# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 104
def cookbook_version_paths
  slurp_metadata! if @cookbook_version_paths.nil?
  @cookbook_version_paths
end
path=(path) click to toggle source

Setter for setting the path. It may either be a full chef-repo with a cookbooks directory in it, or only a path to the cookbooks directory, and it autodetects which it is passed.

@param path [String] path to a chef-repo or the cookbook path under it

# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 91
def path=(path)
  cookbooks_path = "#{path}/cookbooks"
  if Dir.exist?(cookbooks_path)
    @path = cookbooks_path
  else
    @path = path
  end
end
slurp_metadata!() click to toggle source

Helper to compute the @universe_graph and @cookbook_version_paths once from the Chef::CookbookLoader on-disk cookbook repo.

# File lib/chef-cli/policyfile/chef_repo_cookbook_source.rb, line 111
def slurp_metadata!
  @universe_graph = {}
  @cookbook_version_paths = {}
  cookbook_repo.load_cookbooks
  cookbook_repo.each do |cookbook_name, cookbook_version|
    metadata = cookbook_version.metadata
    if metadata.name.nil?
      ui.err("WARN: #{cookbook_name} cookbook missing metadata or no name field, skipping")
      next
    end
    @universe_graph[metadata.name] ||= {}
    @universe_graph[metadata.name][metadata.version] = metadata.dependencies.to_a
    @cookbook_version_paths[metadata.name] ||= {}
    @cookbook_version_paths[metadata.name][metadata.version] = cookbook_version.root_dir
  end
end