|
| 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 'json' |
| 21 | +require 'uri' |
| 22 | + |
| 23 | +class Chef |
| 24 | + class Knife |
| 25 | + class CookbookShow < Knife |
| 26 | + |
| 27 | + banner "Sub-Command: cookbook show COOKBOOK [PART] [FILENAME] (options)" |
| 28 | + |
| 29 | + option :fqdn, |
| 30 | + :short => "-f FQDN", |
| 31 | + :long => "--fqdn FQDN", |
| 32 | + :description => "The FQDN of the host to see the file for" |
| 33 | + |
| 34 | + option :platform, |
| 35 | + :short => "-p PLATFORM", |
| 36 | + :long => "--platform PLATFORM", |
| 37 | + :description => "The platform to see the file for" |
| 38 | + |
| 39 | + option :platform_version, |
| 40 | + :short => "-V VERSION", |
| 41 | + :long => "--platform-version VERSION", |
| 42 | + :description => "The platform version to see the file for" |
| 43 | + |
| 44 | + def run |
| 45 | + case @name_args.length |
| 46 | + when 3 # We are showing a specific file |
| 47 | + arguments = { :id => @name_args[2] } |
| 48 | + arguments[:fqdn] = config[:fqdn] if config.has_key?(:fqdn) |
| 49 | + arguments[:platform] = config[:platform] if config.has_key?(:platform) |
| 50 | + arguments[:version] = config[:platform_version] if config.has_key?(:platform_version) |
| 51 | + result = rest.get_rest("cookbooks/#{@name_args[0]}/#{@name_args[1]}?#{make_query_params(arguments)}") |
| 52 | + puts result |
| 53 | + when 2 # We are showing a specific part of the cookbook |
| 54 | + result = rest.get_rest("cookbooks/#{@name_args[0]}") |
| 55 | + json_pretty_print(result[@name_args[1]]) |
| 56 | + when 1 # We are showing the whole cookbook data |
| 57 | + json_pretty_print(rest.get_rest("cookbooks/#{@name_args[0]}")) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + def make_query_params(req_opts) |
| 62 | + query_part = "" |
| 63 | + req_opts.each do |key, value| |
| 64 | + query_part << "#{key}=#{URI.escape(value)}" |
| 65 | + end |
| 66 | + query_part |
| 67 | + end |
| 68 | + |
| 69 | + end |
| 70 | + end |
| 71 | +end |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
0 commit comments