Class: Chef::Knife::Search

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/search.rb

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, build_sub_class, #bulk_delete, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, find_command, #format_for_display, #format_list_for_display, #json_pretty_print, list_commands, load_commands, #load_from_file, #pretty_print, #rest

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string

Instance Method Details

#runObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/knife/search.rb', line 63

def run 
  q = Chef::Search::Query.new
  display = { :total => 0, :start => config[:start] ? config[:start] : 0, :rows => [ ] }

  q.search(@name_args[0], @name_args[1], config[:sort], config[:start] ? config[:start] : 0, config[:rows] ? config[:rows] : 20) do |item|
    formatted_item = format_for_display(item)
    if formatted_item.respond_to?(:has_key?) && !formatted_item.has_key?('id')
      formatted_item['id'] = item.has_key?('id') ? item['id'] : item.name 
    end
    display[:rows] << formatted_item
    display[:total] += 1
  end

  if config[:id_only]
    if config[:attribute]
      display[:rows].each do |row|
        puts row[config[:attribute]] if row.has_key?(config[:attribute]) && !row[config[:attribute]].nil?
      end
    else
      puts display[:rows].join("\n") 
    end
  else
    json_pretty_print(display)
  end
end