|
| 1 | +# |
| 2 | +# Author:: Adam Jacob (<adam@opscode.com>) |
| 3 | +# Copyright:: Copyright (c) 2009 Opscode, Inc. |
| 4 | +# License:: Apache License, Version 2.0 |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +require 'chef/knife' |
| 20 | +require 'chef/data_bag_item' |
| 21 | + |
| 22 | +class Chef |
| 23 | + class Knife |
| 24 | + class Search < Knife |
| 25 | + |
| 26 | + banner "Sub-Command: search INDEX QUERY (options)" |
| 27 | + |
| 28 | + option :sort, |
| 29 | + :short => "-o SORT", |
| 30 | + :long => "--sort SORT", |
| 31 | + :description => "The order to sort the results in", |
| 32 | + :default => nil |
| 33 | + |
| 34 | + option :start, |
| 35 | + :short => "-b ROW", |
| 36 | + :long => "--start ROW", |
| 37 | + :description => "The row to start returning results at", |
| 38 | + :default => nil, |
| 39 | + :proc => lambda { |i| i.to_i } |
| 40 | + |
| 41 | + option :rows, |
| 42 | + :short => "-R INT", |
| 43 | + :long => "--rows INT", |
| 44 | + :description => "The number of rows to return", |
| 45 | + :default => nil, |
| 46 | + :proc => lambda { |i| i.to_i } |
| 47 | + |
| 48 | + option :attribute, |
| 49 | + :short => "-a [ATTR]", |
| 50 | + :long => "--attribute [ATTR]", |
| 51 | + :description => "Show only one attribute" |
| 52 | + |
| 53 | + option :run_list, |
| 54 | + :short => "-r", |
| 55 | + :long => "--run-list", |
| 56 | + :description => "Show only the run list" |
| 57 | + |
| 58 | + option :id_only, |
| 59 | + :short => "-i", |
| 60 | + :long => "--id-only", |
| 61 | + :description => "Show only the ID of matching objects" |
| 62 | + |
| 63 | + def run |
| 64 | + q = Chef::Search::Query.new |
| 65 | + display = { :total => 0, :start => config[:start] ? config[:start] : 0, :rows => [ ] } |
| 66 | + |
| 67 | + q.search(@name_args[0], @name_args[1], config[:sort], config[:start], config[:rows]) do |item| |
| 68 | + formatted_item = format_for_display(item) |
| 69 | + if formatted_item.respond_to?(:has_key?) && !formatted_item.has_key?('id') |
| 70 | + formatted_item['id'] = item.has_key?('id') ? item['id'] : item.name |
| 71 | + end |
| 72 | + display[:rows] << formatted_item |
| 73 | + display[:total] += 1 |
| 74 | + end |
| 75 | + |
| 76 | + if config[:id_only] |
| 77 | + if config[:attribute] |
| 78 | + display[:rows].each do |row| |
| 79 | + puts row[config[:attribute]] if row.has_key?(config[:attribute]) && !row[config[:attribute]].nil? |
| 80 | + end |
| 81 | + else |
| 82 | + puts display[:rows].join("\n") |
| 83 | + end |
| 84 | + else |
| 85 | + json_pretty_print(display) |
| 86 | + end |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | +end |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
0 commit comments