From 3715c10b2b66031a7e05bd437f48e3817b755ed7 Mon Sep 17 00:00:00 2001 From: Martin Smith Date: Thu, 9 Oct 2014 15:30:15 -0500 Subject: Allow `knife cookbook site share` to omit category This adds a feature for knife to lookup the category on an existing cookbook. This enables uploading new versions of existing cookbooks with much less pain. --- lib/chef/knife/cookbook_site_share.rb | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'lib/chef/knife/cookbook_site_share.rb') diff --git a/lib/chef/knife/cookbook_site_share.rb b/lib/chef/knife/cookbook_site_share.rb index 7204ccdb1c..9f345711ff 100644 --- a/lib/chef/knife/cookbook_site_share.rb +++ b/lib/chef/knife/cookbook_site_share.rb @@ -41,16 +41,25 @@ class Chef :proc => lambda { |o| Chef::Config.cookbook_path = o.split(":") } def run - if @name_args.length < 2 + config[:cookbook_path] ||= Chef::Config[:cookbook_path] + + if @name_args.length < 1 show_usage ui.fatal("You must specify the cookbook name and the category you want to share this cookbook to.") - exit 1 + exit(1) + elsif @name_args.length < 2 + cookbook_name = @name_args[0] + category = get_category(cookbook_name) + if category.nil? || category.empty? + show_usage + ui.fatal("You must specify the cookbook name and the category you want to share this cookbook to.") + exit(1) + end + else + cookbook_name = @name_args[0] + category = @name_args[1] end - config[:cookbook_path] ||= Chef::Config[:cookbook_path] - - cookbook_name = @name_args[0] - category = @name_args[1] cl = Chef::CookbookLoader.new(config[:cookbook_path]) if cl.cookbook_exists?(cookbook_name) cookbook = cl[cookbook_name] @@ -84,6 +93,17 @@ class Chef end + def get_category(cookbook_name) + begin + data = noauth_rest.get_rest("http://cookbooks.opscode.com/api/v1/cookbooks/#{@name_args[0]}") + data["category"] + rescue => e + ui.fatal("Error fetching cookbook to determine category") + Chef::Log.debug("\n#{e.backtrace.join("\n")}") + exit(1) + end + end + def do_upload(cookbook_filename, cookbook_category, user_id, user_secret_filename) uri = "https://supermarket.getchef.com/api/v1/cookbooks" -- cgit v1.2.1 From bb278f43e68c5b71fc0ad6009d14a1a10cd9b776 Mon Sep 17 00:00:00 2001 From: Martin Smith Date: Fri, 10 Oct 2014 14:16:01 -0500 Subject: Adjust banner, error messages Per feedback from @sersut, distinguish between excption and simply Supermarket error or cookbook not found. --- lib/chef/knife/cookbook_site_share.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'lib/chef/knife/cookbook_site_share.rb') diff --git a/lib/chef/knife/cookbook_site_share.rb b/lib/chef/knife/cookbook_site_share.rb index 9f345711ff..bcdd782be2 100644 --- a/lib/chef/knife/cookbook_site_share.rb +++ b/lib/chef/knife/cookbook_site_share.rb @@ -31,7 +31,7 @@ class Chef require 'chef/cookbook_site_streaming_uploader' end - banner "knife cookbook site share COOKBOOK CATEGORY (options)" + banner "knife cookbook site share COOKBOOK [CATEGORY] (options)" category "cookbook site" option :cookbook_path, @@ -45,16 +45,11 @@ class Chef if @name_args.length < 1 show_usage - ui.fatal("You must specify the cookbook name and the category you want to share this cookbook to.") + ui.fatal("You must specify the cookbook name.") exit(1) elsif @name_args.length < 2 cookbook_name = @name_args[0] category = get_category(cookbook_name) - if category.nil? || category.empty? - show_usage - ui.fatal("You must specify the cookbook name and the category you want to share this cookbook to.") - exit(1) - end else cookbook_name = @name_args[0] category = @name_args[1] @@ -96,9 +91,14 @@ class Chef def get_category(cookbook_name) begin data = noauth_rest.get_rest("http://cookbooks.opscode.com/api/v1/cookbooks/#{@name_args[0]}") - data["category"] + if !data["category"] && data["error_code"] + ui.fatal("Received an error from the Opscode Cookbook site: #{data["error_code"]}. On the first time you upload it, you are required to specify the category you want to share this cookbook to.") + exit(1) + else + data['category'] + end rescue => e - ui.fatal("Error fetching cookbook to determine category") + ui.fatal("Unable to reach Opscode Cookbook Site: #{e.message}. Increase log verbosity (-VV) for more information.") Chef::Log.debug("\n#{e.backtrace.join("\n")}") exit(1) end -- cgit v1.2.1