Commit 421517d3 authored by Daniel DeLeo's avatar Daniel DeLeo
Browse files

Merge branch 'CHEF-2147'

parents a6829e5c dca02d8e
......@@ -84,7 +84,7 @@ class Chef
output = edit_item(item)
rest.put_rest("data/#{@name_args[0]}/#{@name_args[1]}", output)
stdout.puts("Saved data_bag_item[#{@name_args[1]}]")
output(format_for_display(object)) if config[:print_after]
output(format_for_display(object.raw_data)) if config[:print_after]
end
end
end
......
......@@ -58,21 +58,21 @@ class Chef
def run
display = case @name_args.length
when 2
if use_encryption
raw = Chef::EncryptedDataBagItem.load(@name_args[0],
@name_args[1],
read_secret)
format_for_display(raw.to_hash)
else
format_for_display(Chef::DataBagItem.load(@name_args[0], @name_args[1]))
end
when 1
format_list_for_display(Chef::DataBag.load(@name_args[0]))
else
stdout.puts opt_parser
exit(1)
end
when 2
if use_encryption
raw = Chef::EncryptedDataBagItem.load(@name_args[0],
@name_args[1],
read_secret)
format_for_display(raw.to_hash)
else
format_for_display(Chef::DataBagItem.load(@name_args[0], @name_args[1]).raw_data)
end
when 1
format_list_for_display(Chef::DataBag.load(@name_args[0]))
else
stdout.puts opt_parser
exit(1)
end
output(display)
end
end
......
......@@ -114,9 +114,7 @@ class Chef
config[:with_uri] ? list : list.keys.sort { |a,b| a <=> b }
end
def format_for_display(item)
data = item.kind_of?(Chef::DataBagItem) ? item.raw_data : item
def format_for_display(data)
if config[:attribute]
config[:attribute].split(".").each do |attr|
if data.respond_to?(:[])
......@@ -127,12 +125,12 @@ class Chef
data = data.send(attr.to_sym)
end
end
{ config[:attribute] => data.kind_of?(Chef::Node::Attribute) ? data.to_hash : data }
{ config[:attribute] => data.respond_to?(:to_hash) ? data.to_hash : data }
elsif config[:run_list]
data = data.run_list.run_list
{ "run_list" => data }
elsif config[:environment]
if data.class == Chef::Node
if data.respond_to?(:chef_environment)
{"chef_environment" => data.chef_environment}
else
# this is a place holder for now. Feel free to modify (i.e. add other cases). [nuo]
......
......@@ -50,12 +50,12 @@ describe Chef::Knife::DataBagShow do
it "prints the contents of the data bag item when given a bag and item name" do
@knife.instance_variable_set(:@name_args, ['bag_o_data', 'an_item'])
data_item_content = {"id" => "an_item", "zsh" => "victory_through_tabbing"}
data_item = Chef::DataBagItem.new.tap {|item| item.raw_data = {"id" => "an_item", "zsh" => "victory_through_tabbing"}}
Chef::DataBagItem.should_receive(:load).with('bag_o_data', 'an_item').and_return(data_item_content)
Chef::DataBagItem.should_receive(:load).with('bag_o_data', 'an_item').and_return(data_item)
@knife.run
Chef::JSONCompat.from_json(@stdout.string).should == data_item_content
Chef::JSONCompat.from_json(@stdout.string).should == data_item.raw_data
end
describe "encrypted data bag items" do
......
......@@ -45,14 +45,6 @@ describe Chef::Knife::UI do
@ui.format_for_display(input).should == input
end
describe "with a data bag item" do
it "should use the raw data" do
dbi = mock(Chef::DataBagItem, :kind_of? => true)
dbi.should_receive(:raw_data).and_return({ "monkey" => "soup" })
@ui.format_for_display(dbi).should == { "monkey" => "soup" }
end
end
describe "with --attribute passed" do
it "should return the deeply nested attribute" do
input = { "gi" => { "go" => "ge" } }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment