lib/chef/knife/cookbook_cleanup.rb in knife-cookbook-cleanup-1.0.0 vs lib/chef/knife/cookbook_cleanup.rb in knife-cookbook-cleanup-1.1.0

- old
+ new

@@ -27,11 +27,11 @@ require 'chef/knife/bootstrap' require 'chef/cookbook_version' Chef::Knife::Bootstrap.load_deps end - banner 'knife cookbook cleanup' + banner 'knife cookbook cleanup [COOKBOOK] (options)' option :versions_to_keep, long: '--versions-to-keep VALUE', description: 'Versions of cookbooks to keep (default 1)', default: 1, @@ -42,14 +42,16 @@ description: 'Allows showing what would have been done without actually doing the deed', boolean: true, default: false def run + target_cookbook = name_args[0] if name_args.length.positive? all_cookbooks = rest.get_rest('/cookbooks?num_versions=all') drop_cookbooks = {} keep_cookbooks = {} all_cookbooks.each do |cb| cookbook_name = cb[0] + next if !target_cookbook.nil? && target_cookbook != cookbook_name sorted_versions = cb[1]['versions'].map { |v| v['version'] }.sort { |x, y| Gem::Version.new(x) <=> Gem::Version.new(y) } keep_versions = sorted_versions.pop(config[:versions_to_keep]) dropped_versions = sorted_versions drop_cookbooks[cookbook_name] = dropped_versions keep_cookbooks[cookbook_name] = keep_versions