Class: Chef::Knife::Core::GenericPresenter
- Inherits:
-
Object
- Object
- Chef::Knife::Core::GenericPresenter
- Defined in:
- lib/chef/knife/core/generic_presenter.rb
Overview
The base presenter class for displaying structured data in knife commands. This is not an abstract base class, and it is suitable for displaying most kinds of objects that knife needs to display.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#ui ⇒ Object
readonly
Returns the value of attribute ui.
Instance Method Summary collapse
-
#attribute_field_separator ⇒ Object
GenericPresenter is used in contexts where MultiAttributeReturnOption is not, so we need to set the default value here rather than as part of the CLI option.
- #extract_nested_value(data, nested_value_spec) ⇒ Object
-
#format(data) ⇒ Object
Returns a String representation of
data
that is suitable for output to a terminal or perhaps for data interchange with another program. - #format_cookbook_list_for_display(item) ⇒ Object
- #format_data_subset_for_display(data) ⇒ Object
- #format_for_display(data) ⇒ Object
- #format_list_for_display(list) ⇒ Object
- #formatting_subset_of_data? ⇒ Boolean
-
#initialize(ui, config) ⇒ GenericPresenter
constructor
Instaniates a new GenericPresenter.
-
#interchange? ⇒ Boolean
Is the selected output format a data interchange format? Returns true if the selected output format is json or yaml, false otherwise.
- #name_or_id_for(data) ⇒ Object
-
#parse_format_option ⇒ Object
Converts the user-supplied value of ‘config` to a Symbol representing the desired output format.
-
#summarize(data) ⇒ Object
Summarize the data.
-
#text_format(data) ⇒ Object
Converts the
data
to a String in the text format.
Constructor Details
#initialize(ui, config) ⇒ GenericPresenter
Instaniates a new GenericPresenter. This is generally handled by the Chef::Knife::UI object, though you need to match the signature of this method if you intend to use your own presenter instead.
59 60 61 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 59 def initialize(ui, config) @ui, @config = ui, config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
54 55 56 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 54 def config @config end |
#ui ⇒ Object (readonly)
Returns the value of attribute ui.
53 54 55 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 53 def ui @ui end |
Instance Method Details
#attribute_field_separator ⇒ Object
GenericPresenter is used in contexts where MultiAttributeReturnOption is not, so we need to set the default value here rather than as part of the CLI option.
185 186 187 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 185 def attribute_field_separator config[:field_separator] || "." end |
#extract_nested_value(data, nested_value_spec) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 189 def extract_nested_value(data, nested_value_spec) nested_value_spec.split(attribute_field_separator).each do |attr| data = if data.is_a?(Array) data[attr.to_i] elsif data.respond_to?(:[], false) && data.respond_to?(:key?) && data.key?(attr) data[attr] elsif data.respond_to?(attr.to_sym, false) # handles -a chef_environment and other things that hang of the node and aren't really attributes data.public_send(attr.to_sym) else nil end end # necessary (?) for coercing objects (the run_list object?) to hashes ( !data.is_a?(Array) && data.respond_to?(:to_hash) ) ? data.to_hash : data end |
#format(data) ⇒ Object
Returns a String representation of data
that is suitable for output to a terminal or perhaps for data interchange with another program. The representation of the data
depends on the value of the ‘config` setting.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 80 def format(data) case parse_format_option when :summary summarize(data) when :text text_format(data) when :json Chef::JSONCompat.to_json_pretty(data) when :yaml require "yaml" YAML.dump(data) when :pp require "stringio" unless defined?(StringIO) # If you were looking for some attribute and there is only one match # just dump the attribute value if config[:attribute] && data.length == 1 data.values[0] else out = StringIO.new PP.pp(data, out) out.string end end end |
#format_cookbook_list_for_display(item) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 207 def format_cookbook_list_for_display(item) if config[:with_uri] item.inject({}) do |collected, (cookbook, versions)| collected[cookbook] = {} versions["versions"].each do |ver| collected[cookbook][ver["version"]] = ver["url"] end collected end else versions_by_cookbook = item.inject({}) do |collected, ( cookbook, versions )| collected[cookbook] = versions["versions"].map { |v| v["version"] } collected end key_length = versions_by_cookbook.empty? ? 0 : versions_by_cookbook.keys.map(&:size).max + 2 versions_by_cookbook.sort.map do |cookbook, versions| "#{cookbook.ljust(key_length)} #{versions.join(" ")}" end end end |
#format_data_subset_for_display(data) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 157 def format_data_subset_for_display(data) subset = if config[:attribute] result = {} Array(config[:attribute]).each do |nested_value_spec| nested_value = extract_nested_value(data, nested_value_spec) result[nested_value_spec] = nested_value end result elsif config[:run_list] run_list = data.run_list.run_list { "run_list" => run_list } else raise ArgumentError, "format_data_subset_for_display requires attribute, run_list, or id_only config option to be set" end { name_or_id_for(data) => subset } end |
#format_for_display(data) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 145 def format_for_display(data) if formatting_subset_of_data? format_data_subset_for_display(data) elsif config[:id_only] name_or_id_for(data) elsif config[:environment] && data.respond_to?(:chef_environment) { "chef_environment" => data.chef_environment } else data end end |
#format_list_for_display(list) ⇒ Object
141 142 143 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 141 def format_list_for_display(list) config[:with_uri] ? list : list.keys.sort { |a, b| a <=> b } end |
#formatting_subset_of_data? ⇒ Boolean
178 179 180 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 178 def formatting_subset_of_data? config[:attribute] || config[:run_list] end |
#interchange? ⇒ Boolean
Is the selected output format a data interchange format? Returns true if the selected output format is json or yaml, false otherwise. Knife search uses this to adjust its data output so as not to produce invalid JSON output.
67 68 69 70 71 72 73 74 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 67 def interchange? case parse_format_option when :json, :yaml true else false end end |
#name_or_id_for(data) ⇒ Object
174 175 176 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 174 def name_or_id_for(data) data.respond_to?(:name) ? data.name : data["id"] end |
#parse_format_option ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 112 def parse_format_option case config[:format] when "summary", /^s/, nil :summary when "text", /^t/ :text when "json", /^j/ :json when "yaml", /^y/ :yaml when "pp", /^p/ :pp else raise ArgumentError, "Unknown output format #{config[:format]}" end end |
#summarize(data) ⇒ Object
Summarize the data. Defaults to text format output, which may not be very summary-like
131 132 133 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 131 def summarize(data) text_format(data) end |
#text_format(data) ⇒ Object
Converts the data
to a String in the text format. Uses Chef::Knife::Core::TextFormatter
137 138 139 |
# File 'lib/chef/knife/core/generic_presenter.rb', line 137 def text_format(data) TextFormatter.new(data, ui).formatted_data end |