diff options
author | John Keiser <jkeiser@opscode.com> | 2013-05-02 17:19:36 -0700 |
---|---|---|
committer | John Keiser <jkeiser@opscode.com> | 2013-06-07 13:12:32 -0700 |
commit | 4b33978d9aa40ea66db16b2f985f51f19bdacf8d (patch) | |
tree | 4ed868b133ad06fa040c3b9f96e1f9ff48f13db7 /lib/chef | |
parent | c2e7301ee0225a92c231d6756fa5c1bb39e5a13e (diff) | |
download | chef-4b33978d9aa40ea66db16b2f985f51f19bdacf8d.tar.gz |
Turn list() and list_pairs() into Enumerables
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/chef_fs/command_line.rb | 2 | ||||
-rw-r--r-- | lib/chef/chef_fs/file_system.rb | 102 | ||||
-rw-r--r-- | lib/chef/knife/delete.rb | 6 | ||||
-rw-r--r-- | lib/chef/knife/deps.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/edit.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/list.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/show.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/xargs.rb | 2 |
8 files changed, 78 insertions, 42 deletions
diff --git a/lib/chef/chef_fs/command_line.rb b/lib/chef/chef_fs/command_line.rb index f4d3abd51e..6236871a52 100644 --- a/lib/chef/chef_fs/command_line.rb +++ b/lib/chef/chef_fs/command_line.rb @@ -126,7 +126,7 @@ class Chef end def self.diff(pattern, a_root, b_root, recurse_depth, get_content) - Chef::ChefFS::FileSystem.list_pairs(pattern, a_root, b_root) do |a, b| + Chef::ChefFS::FileSystem.list_pairs(pattern, a_root, b_root).each do |a, b| diff_entries(a, b, recurse_depth, get_content) do |diff| yield diff end diff --git a/lib/chef/chef_fs/file_system.rb b/lib/chef/chef_fs/file_system.rb index 8ab9a7ceda..3c7fd3c155 100644 --- a/lib/chef/chef_fs/file_system.rb +++ b/lib/chef/chef_fs/file_system.rb @@ -24,36 +24,54 @@ require 'chef/chef_fs/file_system/operation_not_allowed_error' class Chef module ChefFS module FileSystem - # Yields a list of all things under (and including) this entry that match the + # Returns a list of all things under (and including) this entry that match the # given pattern. # # ==== Attributes # - # * +entry+ - Entry to start listing under + # * +root+ - Entry to start listing under # * +pattern+ - Chef::ChefFS::FilePattern to match children under # - def self.list(entry, pattern, &block) - # Include self in results if it matches - if pattern.match?(entry.path) - block.call(entry) + def self.list(root, pattern) + Lister.new(root, pattern) + end + + class Lister + include Enumerable + + def initialize(root, pattern) + @root = root + @pattern = pattern end - if pattern.could_match_children?(entry.path) - # If it's possible that our children could match, descend in and add matches. - exact_child_name = pattern.exact_child_name_under(entry.path) + attr_reader :root + attr_reader :pattern - # If we've got an exact name, don't bother listing children; just grab the - # child with the given name. - if exact_child_name - exact_child = entry.child(exact_child_name) - if exact_child - list(exact_child, pattern, &block) - end + def each + list_from(root) { |entry| yield entry } + end - # Otherwise, go through all children and find any matches - elsif entry.dir? - entry.children.each do |child| - list(child, pattern, &block) + def list_from(entry, &block) + # Include self in results if it matches + if pattern.match?(entry.path) + block.call(entry) + end + + if pattern.could_match_children?(entry.path) + # If it's possible that our children could match, descend in and add matches. + exact_child_name = pattern.exact_child_name_under(entry.path) + + # If we've got an exact name, don't bother listing children; just grab the + # child with the given name. + if exact_child_name + exact_child = entry.child(exact_child_name) + if exact_child + list_from(exact_child, &block) + end + + # Otherwise, go through all children and find any matches + elsif entry.dir? + entry.children.each { |child| list_from(child, &block) } end end end @@ -119,7 +137,7 @@ class Chef def self.copy_to(pattern, src_root, dest_root, recurse_depth, options, ui, format_path) found_result = false error = false - list_pairs(pattern, src_root, dest_root) do |src, dest| + list_pairs(pattern, src_root, dest_root).each do |src, dest| found_result = true new_dest_parent = get_or_create_parent(dest, options, ui, format_path) child_error = copy_entries(src, dest, new_dest_parent, recurse_depth, options, ui, format_path) @@ -143,26 +161,44 @@ class Chef # # ==== Example # - # Chef::ChefFS::FileSystem.list_pairs(FilePattern.new('**x.txt', a_root, b_root)) do |a, b| + # Chef::ChefFS::FileSystem.list_pairs(FilePattern.new('**x.txt', a_root, b_root)).each do |a, b| # ... # end # def self.list_pairs(pattern, a_root, b_root) - # Make sure everything on the server is also on the filesystem, and diff - found_paths = Set.new - Chef::ChefFS::FileSystem.list(a_root, pattern) do |a| - found_paths << a.path - b = Chef::ChefFS::FileSystem.resolve_path(b_root, a.path) - yield [ a, b ] + PairLister.new(pattern, a_root, b_root) + end + + class PairLister + include Enumerable + + def initialize(pattern, a_root, b_root) + @pattern = pattern + @a_root = a_root + @b_root = b_root end - # Check the outer regex pattern to see if it matches anything on the - # filesystem that isn't on the server - Chef::ChefFS::FileSystem.list(b_root, pattern) do |b| - if !found_paths.include?(b.path) - a = Chef::ChefFS::FileSystem.resolve_path(a_root, b.path) + attr_reader :pattern + attr_reader :a_root + attr_reader :b_root + + def each + # Make sure everything on the server is also on the filesystem, and diff + found_paths = Set.new + Chef::ChefFS::FileSystem.list(a_root, pattern).each do |a| + found_paths << a.path + b = Chef::ChefFS::FileSystem.resolve_path(b_root, a.path) yield [ a, b ] end + + # Check the outer regex pattern to see if it matches anything on the + # filesystem that isn't on the server + Chef::ChefFS::FileSystem.list(b_root, pattern).each do |b| + if !found_paths.include?(b.path) + a = Chef::ChefFS::FileSystem.resolve_path(a_root, b.path) + yield [ a, b ] + end + end end end diff --git a/lib/chef/knife/delete.rb b/lib/chef/knife/delete.rb index 417336b56b..cf65b8994b 100644 --- a/lib/chef/knife/delete.rb +++ b/lib/chef/knife/delete.rb @@ -36,7 +36,7 @@ class Chef error = false if config[:remote_only] pattern_args.each do |pattern| - Chef::ChefFS::FileSystem.list(chef_fs, pattern) do |result| + Chef::ChefFS::FileSystem.list(chef_fs, pattern).each do |result| if delete_result(result) error = true end @@ -44,7 +44,7 @@ class Chef end elsif config[:local_only] pattern_args.each do |pattern| - Chef::ChefFS::FileSystem.list(local_fs, pattern) do |result| + Chef::ChefFS::FileSystem.list(local_fs, pattern).each do |result| if delete_result(result) error = true end @@ -52,7 +52,7 @@ class Chef end else pattern_args.each do |pattern| - Chef::ChefFS::FileSystem.list_pairs(pattern, chef_fs, local_fs) do |chef_result, local_result| + Chef::ChefFS::FileSystem.list_pairs(pattern, chef_fs, local_fs).each do |chef_result, local_result| if delete_result(chef_result, local_result) error = true end diff --git a/lib/chef/knife/deps.rb b/lib/chef/knife/deps.rb index 2e8effdf06..0c36759a12 100644 --- a/lib/chef/knife/deps.rb +++ b/lib/chef/knife/deps.rb @@ -34,7 +34,7 @@ class Chef @root = config[:remote] ? chef_fs : local_fs dependencies = {} pattern_args.each do |pattern| - Chef::ChefFS::FileSystem.list(@root, pattern) do |entry| + Chef::ChefFS::FileSystem.list(@root, pattern).each do |entry| if config[:tree] print_dependencies_tree(entry, dependencies) else diff --git a/lib/chef/knife/edit.rb b/lib/chef/knife/edit.rb index ba05c61053..79e46957ca 100644 --- a/lib/chef/knife/edit.rb +++ b/lib/chef/knife/edit.rb @@ -18,7 +18,7 @@ class Chef # Get the matches (recursively) error = false pattern_args.each do |pattern| - Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern) do |result| + Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern).each do |result| if result.dir? ui.error "#{format_path(result)}: is a directory" if pattern.exact_path error = true diff --git a/lib/chef/knife/list.rb b/lib/chef/knife/list.rb index d573112a53..dca30aca46 100644 --- a/lib/chef/knife/list.rb +++ b/lib/chef/knife/list.rb @@ -44,7 +44,7 @@ class Chef results = [] dir_results = [] pattern_args_from(patterns).each do |pattern| - Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern) do |result| + Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern).each do |result| if result.dir? && !config[:bare_directories] dir_results += add_dir_result(result) elsif result.exists? diff --git a/lib/chef/knife/show.rb b/lib/chef/knife/show.rb index df9a8b6516..f838ed466a 100644 --- a/lib/chef/knife/show.rb +++ b/lib/chef/knife/show.rb @@ -18,7 +18,7 @@ class Chef # Get the matches (recursively) error = false pattern_args.each do |pattern| - Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern) do |result| + Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern).each do |result| if result.dir? ui.error "#{format_path(result)}: is a directory" if pattern.exact_path error = true diff --git a/lib/chef/knife/xargs.rb b/lib/chef/knife/xargs.rb index 07895d70df..0a34f380ce 100644 --- a/lib/chef/knife/xargs.rb +++ b/lib/chef/knife/xargs.rb @@ -72,7 +72,7 @@ class Chef # Get the matches (recursively) files = [] pattern_args_from(get_patterns).each do |pattern| - Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern) do |result| + Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern).each do |result| if result.dir? # TODO option to include directories ui.warn "#{format_path(result)}: is a directory. Will not run #{command} on it." |