Class: ChefCLI::Policyfile::GitLockFetcher
- Inherits:
-
Object
- Object
- ChefCLI::Policyfile::GitLockFetcher
- Defined in:
- lib/chef-cli/policyfile/git_lock_fetcher.rb
Overview
A Policyfile lock fetcher that can read a lock file from a git repository.
Instance Attribute Summary collapse
- #branch ⇒ Object readonly
- #name ⇒ Object
- #path ⇒ Object readonly
- #ref ⇒ Object readonly
- #revision ⇒ Object readonly
- #source_options ⇒ Object
- #storage_config ⇒ Object
- #tag ⇒ Object readonly
- #uri ⇒ Object readonly
Instance Method Summary collapse
-
#apply_locked_source_options(options_from_lock) ⇒ Object
Applies source options from a lock file.
-
#errors ⇒ Array<String>
Check the options provided when craeting this class for errors.
-
#initialize(name, source_options, storage_config) ⇒ GitLockFetcher
constructor
Initialize a GitLockFetcher.
-
#lock_data ⇒ Hash
Of the policyfile lock data.
-
#source_options_for_lock ⇒ Hash
The source_options that describe how to fetch this exact lock again.
- #valid? ⇒ True, False
Constructor Details
#initialize(name, source_options, storage_config) ⇒ GitLockFetcher
Initialize a GitLockFetcher
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 54 def initialize(name, , storage_config) @name = name @storage_config = storage_config @source_options = symbolize_keys() @revision = @source_options[:revision] @path = @source_options[:path] || @source_options[:rel] @uri = @source_options[:git] @branch = @source_options[:branch] @tag = @source_options[:tag] @ref = @source_options[:ref] # The revision to parse @rev_parse = @source_options[:ref] || @source_options[:branch] || @source_options[:tag] || "master" end |
Instance Attribute Details
#branch ⇒ Object (readonly)
45 46 47 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 45 def branch @branch end |
#name ⇒ Object
38 39 40 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 38 def name @name end |
#path ⇒ Object (readonly)
44 45 46 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 44 def path @path end |
#ref ⇒ Object (readonly)
47 48 49 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 47 def ref @ref end |
#revision ⇒ Object (readonly)
43 44 45 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 43 def revision @revision end |
#source_options ⇒ Object
39 40 41 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 39 def @source_options end |
#storage_config ⇒ Object
40 41 42 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 40 def storage_config @storage_config end |
#tag ⇒ Object (readonly)
46 47 48 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 46 def tag @tag end |
#uri ⇒ Object (readonly)
42 43 44 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 42 def uri @uri end |
Instance Method Details
#apply_locked_source_options(options_from_lock) ⇒ Object
Applies source options from a lock file. This is used to make sure that the same policyfile lock is loaded that was locked
98 99 100 101 102 103 104 105 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 98 def () = .inject({}) do |acc, (key, value)| acc[key.to_sym] = value acc end .merge!() raise ChefCLI::InvalidLockfile, "Invalid source_options provided from lock data: #{.inspect}" unless valid? end |
#errors ⇒ Array<String>
Check the options provided when craeting this class for errors
78 79 80 81 82 83 84 85 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 78 def errors = [] [:git].each do |key| << "include_policy for #{name} is missing key #{key}" unless [key] end end |
#lock_data ⇒ Hash
Returns of the policyfile lock data.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 108 def lock_data @lock_data ||= fetch_lock_data.tap do |data| data["cookbook_locks"].each do |cookbook_name, cookbook_lock| if cookbook_lock["source_options"].key?("path") cookbook_lock["source_options"].tap do |opt| opt["git"] = uri unless opt.key?("git") opt["revision"] = revision unless opt.key?("revision") opt["branch"] = branch unless opt.key?("branch") || branch.nil? opt["tag"] = tag unless opt.key?("tag") || branch.nil? opt["ref"] = ref unless opt.key?("ref") || ref.nil? path_keys = %w{path rel}.map { |path_key| path_key if opt.key?(path_key) }.compact path_keys.each do |name| # We can safely grab the entire cookbook when the Policyfile defines a cookbook path of itself (".") if opt[name] == "." opt.delete(name) next end # Mutate the path key to a rel key so that we identify the source_type # as a git repo and not a local directory. Git also doesn't like paths # prefixed with `./` and cannot use relative paths outside the repo. # http://rubular.com/r/JYpdYHT19p pattern = %r{(^../)|(^./)} opt["rel"] = opt[name].gsub(pattern, "") end # Delete the path key if present to ensure we use the git source_type opt.delete("path") end end # cookbook_lock["source_options"] end # data["cookbook_locks"].each end # fetch_lock_data.tap @lock_data end |
#source_options_for_lock ⇒ Hash
Returns The source_options that describe how to fetch this exact lock again.
88 89 90 91 92 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 88 def .merge({ revision: revision, }) end |
#valid? ⇒ True, False
71 72 73 |
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 71 def valid? errors.empty? end |