diff options
author | Julian C. Dunn <jdunn@getchef.com> | 2013-12-19 17:36:23 -0500 |
---|---|---|
committer | Bryan McLellan <btm@getchef.com> | 2014-03-19 17:09:55 -0700 |
commit | c799a671f42931d5235fa8b90f04d642811fdd0d (patch) | |
tree | b197a8170e7126c94885aa87ca98fa4157e80f47 /lib/chef/knife/node_run_list_add.rb | |
parent | b57226b4a8c80b865cd3e256923ba07e5ac09749 (diff) | |
download | chef-c799a671f42931d5235fa8b90f04d642811fdd0d.tar.gz |
[CHEF-3812] Support a --before option in "knife node run_list add"
Diffstat (limited to 'lib/chef/knife/node_run_list_add.rb')
-rw-r--r-- | lib/chef/knife/node_run_list_add.rb | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/lib/chef/knife/node_run_list_add.rb b/lib/chef/knife/node_run_list_add.rb index dcd41ae997..299ab4e1a5 100644 --- a/lib/chef/knife/node_run_list_add.rb +++ b/lib/chef/knife/node_run_list_add.rb @@ -34,6 +34,11 @@ class Chef :long => "--after ITEM", :description => "Place the ENTRY in the run list after ITEM" + option :before, + :short => "-b ITEM", + :long => "--before ITEM", + :description => "Place the ENTRY in the run list before ITEM" + def run node = Chef::Node.load(@name_args[0]) if @name_args.size > 2 @@ -46,7 +51,17 @@ class Chef entries = @name_args[1].split(',').map { |e| e.strip } end - add_to_run_list(node, entries, config[:after]) + if config[:after] && config[:before] + raise ArgumentError, "You cannot specify both --before and --after" + end + + if config[:after] + add_to_run_list(node, entries, :after, config[:after]) + elsif config[:before] + add_to_run_list(node, entries, :before, config[:before]) + else + add_to_run_list(node, entries) + end node.save @@ -55,13 +70,18 @@ class Chef output(format_for_display(node)) end - def add_to_run_list(node, entries, after=nil) - if after + def add_to_run_list(node, entries, after_or_before=nil, item=nil) + if after_or_before nlist = [] node.run_list.each do |entry| - nlist << entry - if entry == after + if after_or_before == :after && entry == item + nlist << entry + entries.each { |e| nlist << e } + elsif after_or_before == :before && entry == item entries.each { |e| nlist << e } + nlist << entry + else + nlist << entry end end node.run_list.reset!(nlist) |