lib/chef/knife/raw.rb in knife-essentials-0.5.1 vs lib/chef/knife/raw.rb in knife-essentials-0.5.2

- old
+ new

@@ -1,16 +1,24 @@ +require 'json' + class Chef class Knife class Raw < Chef::Knife banner "raw REQUEST_PATH" option :method, :long => '--method METHOD', :short => '-m METHOD', :default => "GET", - :description => "RequestSpecifies the local repository layout. Values: default or full" + :description => "Request method (GET, POST, PUT or DELETE)" + option :pretty, + :long => '--[no-]pretty', + :boolean => true, + :default => true, + :description => "Pretty-print JSON output" + option :input, :long => '--input FILE', :short => '-i FILE', :description => "Name of file to use for PUT or POST" @@ -58,11 +66,14 @@ # response_body = chef_rest.decompress_body(response) response_body = response.body if response.kind_of?(Net::HTTPSuccess) - # This is where we differ from Chef::REST: we return the raw body always. - response_body + if config[:pretty] && response['content-type'] =~ /json/ + JSON.pretty_generate(JSON.parse(response_body, :create_additions => false)) + else + response_body + end elsif redirect_location = redirected_to(response) raise "Redirected to #{create_url(redirect_location)}" follow_redirect {api_request(:GET, create_url(redirect_location))} else # have to decompress the body before making an exception for it. But the body could be nil.