diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/application/face_base.rb | 63 | ||||
-rw-r--r-- | lib/puppet/face/catalog/select.rb | 2 | ||||
-rw-r--r-- | lib/puppet/face/plugin.rb | 2 | ||||
-rw-r--r-- | lib/puppet/interface/action_builder.rb | 2 | ||||
-rw-r--r-- | lib/puppet/network/formats.rb | 36 |
5 files changed, 59 insertions, 46 deletions
diff --git a/lib/puppet/application/face_base.rb b/lib/puppet/application/face_base.rb index 31e58aca4..8c1e03e3f 100644 --- a/lib/puppet/application/face_base.rb +++ b/lib/puppet/application/face_base.rb @@ -29,56 +29,31 @@ class Puppet::Application::FaceBase < Puppet::Application attr_accessor :face, :action, :type, :arguments, :render_as def render_as=(format) - if format == :for_humans or format == :json - @render_as = format - elsif network_format = Puppet::Network::FormatHandler.format(format) - method = network_format.render_method - if method == "to_pson" then - @render_as = :json - else - @render_as = method.to_sym - end - else - raise ArgumentError, "I don't know how to render '#{format}'" - end + @render_as = case format.to_sym + when :for_humans then + # We have an old alias name for :console, which went out in + # 2.7.0rc1, so we are going to carry it forward for a + # while. --daniel 2011-05-04 + Puppet::Network::FormatHandler.format(:console) + when :json then + Puppet::Network::FormatHandler.format(:pson) + else + Puppet::Network::FormatHandler.format(format) + end + @render_as or raise ArgumentError, "I don't know how to render '#{format}'" end def render(result) # Invoke the rendering hook supplied by the user, if appropriate. - if hook = action.when_rendering(render_as) then + if hook = action.when_rendering(render_as.name) + result = hook.call(result) + elsif render_as.name == :console and hook = action.when_rendering(:for_humans) + # We have an old alias name for :console, which went out in 2.7.0rc1, so + # we are going to carry it forward for a while. --daniel 2011-05-04 result = hook.call(result) end - if render_as == :for_humans then - render_for_humans(result) - elsif render_as == :json - PSON::pretty_generate(result, :allow_nan => true, :max_nesting => false) - else - result.send(render_as) - end - end - - def render_for_humans(result) - # String to String - return result if result.is_a? String - return result if result.is_a? Numeric - - # Simple hash to table - if result.is_a? Hash and result.keys.all? { |x| x.is_a? String or x.is_a? Numeric } - output = '' - column_a = result.map do |k,v| k.to_s.length end.max + 2 - column_b = 79 - column_a - result.sort_by { |k,v| k.to_s } .each do |key, value| - output << key.to_s.ljust(column_a) - output << PP.pp(value, '', column_b). - chomp.gsub(/\n */) { |x| x + (' ' * column_a) } - output << "\n" - end - return output - end - - # ...or pretty-print the inspect outcome. - return result.pretty_inspect + render_as.render(result) end def preinit @@ -204,7 +179,7 @@ class Puppet::Application::FaceBase < Puppet::Application @arguments << options # If we don't have a rendering format, set one early. - self.render_as ||= (@action.render_as || :for_humans) + self.render_as ||= (@action.render_as || :console) end diff --git a/lib/puppet/face/catalog/select.rb b/lib/puppet/face/catalog/select.rb index a8ecd82fa..a68a8e0f3 100644 --- a/lib/puppet/face/catalog/select.rb +++ b/lib/puppet/face/catalog/select.rb @@ -15,7 +15,7 @@ Puppet::Face.define(:catalog, '0.0.1') do end end - when_rendering :for_humans do |value| + when_rendering :console do |value| if value.nil? then "no matching resources found" else diff --git a/lib/puppet/face/plugin.rb b/lib/puppet/face/plugin.rb index 4b45ed3a1..8a2559405 100644 --- a/lib/puppet/face/plugin.rb +++ b/lib/puppet/face/plugin.rb @@ -20,7 +20,7 @@ Puppet::Face.define(:plugin, '0.0.1') do Puppet[:pluginsignore]).evaluate end - when_rendering :for_humans do |value| + when_rendering :console do |value| if value.empty? then "No plugins downloaded." else diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb index 0bf4f1408..16305530a 100644 --- a/lib/puppet/interface/action_builder.rb +++ b/lib/puppet/interface/action_builder.rb @@ -38,6 +38,8 @@ class Puppet::Interface::ActionBuilder def render_as(value = nil) value.nil? and raise ArgumentError, "You must give a rendering format to render_as" + # :for_humans is a compatibility alias for :console, but since we shipped + # it in 2.7.0rc1 we need to support it ongoing. --daniel 2011-05-04 formats = Puppet::Network::FormatHandler.formats << :for_humans unless formats.include? value raise ArgumentError, "#{value.inspect} is not a valid rendering format: #{formats.sort.join(", ")}" diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb index 4ca3240d4..082c83ee3 100644 --- a/lib/puppet/network/formats.rb +++ b/lib/puppet/network/formats.rb @@ -160,3 +160,39 @@ end # This is really only ever going to be used for Catalogs. Puppet::Network::FormatHandler.create_serialized_formats(:dot, :required_methods => [:render_method]) + + +Puppet::Network::FormatHandler.create(:console, + :mime => 'text/x-console-text', + :weight => 0) do + def json + @json ||= Puppet::Network::FormatHandler.format(:pson) + end + + def render(datum) + # String to String + return datum if datum.is_a? String + return datum if datum.is_a? Numeric + + # Simple hash to table + if datum.is_a? Hash and datum.keys.all? { |x| x.is_a? String or x.is_a? Numeric } + output = '' + column_a = datum.map do |k,v| k.to_s.length end.max + 2 + column_b = 79 - column_a + datum.sort_by { |k,v| k.to_s } .each do |key, value| + output << key.to_s.ljust(column_a) + output << json.render(value). + chomp.gsub(/\n */) { |x| x + (' ' * column_a) } + output << "\n" + end + return output + end + + # ...or pretty-print the inspect outcome. + return json.render(datum) + end + + def render_multiple(data) + data.collect(&:render).join("\n") + end +end |