diff options
| author | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-27 18:30:27 -0700 |
|---|---|---|
| committer | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-28 10:50:51 -0700 |
| commit | 80adaea6319b2aca81fd3d9b1d0061152b6c7db2 (patch) | |
| tree | cead79a1b4d886fd824157d11b26b189c8a68632 | |
| parent | ebf49f98357f93b33e09c0ecbdee1c5c2db87569 (diff) | |
| download | puppet-80adaea6319b2aca81fd3d9b1d0061152b6c7db2.tar.gz puppet-80adaea6319b2aca81fd3d9b1d0061152b6c7db2.tar.xz puppet-80adaea6319b2aca81fd3d9b1d0061152b6c7db2.zip | |
(#7160) Support 'json' as a rendering method for CLI faces.
We already had some specialized support for rendering JSON using the PSON
libraries; this just extends that to recognize the request on the command line
for json to be identical to a request for pson.
Theoretically we should also support the format in our network rendering code,
but that is a much bigger change, in established code, that has more chance of
destabilizing the whole release.
Reviewed-By: Max Martin <max@puppetlabs.com>
| -rw-r--r-- | lib/puppet/application/face_base.rb | 15 | ||||
| -rwxr-xr-x | spec/unit/application/face_base_spec.rb | 8 |
2 files changed, 17 insertions, 6 deletions
diff --git a/lib/puppet/application/face_base.rb b/lib/puppet/application/face_base.rb index 257d51f75..cc62257c7 100644 --- a/lib/puppet/application/face_base.rb +++ b/lib/puppet/application/face_base.rb @@ -36,15 +36,18 @@ class Puppet::Application::FaceBase < Puppet::Application result = hook.call(result) end + begin + render_method = Puppet::Network::FormatHandler.format(format).render_method + rescue + render_method = nil + end + if format == :for_humans then render_for_humans(result) + elsif format == :json or render_method == "to_pson" + PSON::pretty_generate(result, :allow_nan => true, :max_nesting => false) else - render_method = Puppet::Network::FormatHandler.format(format).render_method - if render_method == "to_pson" - PSON::pretty_generate(result, :allow_nan => true, :max_nesting => false) - else - result.send(render_method) - end + result.send(render_method) end end diff --git a/spec/unit/application/face_base_spec.rb b/spec/unit/application/face_base_spec.rb index 7b3a69f61..e84e6766f 100755 --- a/spec/unit/application/face_base_spec.rb +++ b/spec/unit/application/face_base_spec.rb @@ -296,5 +296,13 @@ EOT app.action.render_as = :for_humans app.render("bi-polar?").should == "bi-winning!" end + + it "should render JSON when asked for json" do + app.action.render_as = :json + json = app.render({ :one => 1, :two => 2 }) + json.should =~ /"one":\s*1\b/ + json.should =~ /"two":\s*2\b/ + PSON.parse(json).should == { "one" => 1, "two" => 2 } + end end end |
