diff options
author | Serdar Sutay <serdar@opscode.com> | 2014-10-10 15:50:51 -0700 |
---|---|---|
committer | Serdar Sutay <serdar@opscode.com> | 2014-10-10 15:50:51 -0700 |
commit | b44a86aab6a4c7e4f8474cebbc0457b673529e67 (patch) | |
tree | 80b80510f8528820228ee4dd4f511adb2fbe621a | |
parent | dcd979cd994d7d9fa630343ed86d7bd9b02bbda2 (diff) | |
parent | bb278f43e68c5b71fc0ad6009d14a1a10cd9b776 (diff) | |
download | chef-b44a86aab6a4c7e4f8474cebbc0457b673529e67.tar.gz |
Merge pull request #2203 from martinb3/master
Allow `knife cookbook site share` to omit category
-rw-r--r-- | lib/chef/knife/cookbook_site_share.rb | 36 | ||||
-rw-r--r-- | spec/unit/knife/cookbook_site_share_spec.rb | 31 |
2 files changed, 57 insertions, 10 deletions
diff --git a/lib/chef/knife/cookbook_site_share.rb b/lib/chef/knife/cookbook_site_share.rb index d6cd570067..b4a7873c71 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, @@ -48,16 +48,20 @@ class Chef :description => "Don't take action, only print what files will be upload to SuperMarket." 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 + 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) + 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] @@ -99,6 +103,22 @@ class Chef end + def get_category(cookbook_name) + begin + data = noauth_rest.get_rest("http://cookbooks.opscode.com/api/v1/cookbooks/#{@name_args[0]}") + 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("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 + end + def do_upload(cookbook_filename, cookbook_category, user_id, user_secret_filename) uri = "https://supermarket.getchef.com/api/v1/cookbooks" diff --git a/spec/unit/knife/cookbook_site_share_spec.rb b/spec/unit/knife/cookbook_site_share_spec.rb index 744472cab7..b85db98d53 100644 --- a/spec/unit/knife/cookbook_site_share_spec.rb +++ b/spec/unit/knife/cookbook_site_share_spec.rb @@ -36,6 +36,9 @@ describe Chef::Knife::CookbookSiteShare do @cookbook_loader.stub(:[]).and_return(@cookbook) Chef::CookbookLoader.stub(:new).and_return(@cookbook_loader) + @noauth_rest = double(Chef::REST) + @knife.stub(:noauth_rest).and_return(@noauth_rest) + @cookbook_uploader = Chef::CookbookUploader.new('herpderp', :rest => "norest") Chef::CookbookUploader.stub(:new).and_return(@cookbook_uploader) @cookbook_uploader.stub(:validate_cookbooks).and_return(true) @@ -50,6 +53,16 @@ describe Chef::Knife::CookbookSiteShare do before(:each) do @knife.stub(:do_upload).and_return(true) + @category_response = { + "name" => "cookbook_name", + "category" => "Testing Category" + } + @bad_category_response = { + "error_code" => "NOT_FOUND", + "error_messages" => [ + "Resource does not exist." + ] + } end it 'should set true to config[:dry_run] as default' do @@ -63,9 +76,23 @@ describe Chef::Knife::CookbookSiteShare do lambda { @knife.run }.should raise_error(SystemExit) end - it 'should print usage and exit when given only 1 argument' do + it 'should not fail when given only 1 argument and can determine category' do @knife.name_args = ['cookbook_name'] - @knife.should_receive(:show_usage) + @noauth_rest.should_receive(:get_rest).with("http://cookbooks.opscode.com/api/v1/cookbooks/cookbook_name").and_return(@category_response) + @knife.should_receive(:do_upload) + @knife.run + end + + it 'should print error and exit when given only 1 argument and cannot determine category' do + @knife.name_args = ['cookbook_name'] + @noauth_rest.should_receive(:get_rest).with("http://cookbooks.opscode.com/api/v1/cookbooks/cookbook_name").and_return(@bad_category_response) + @knife.ui.should_receive(:fatal) + lambda { @knife.run }.should raise_error(SystemExit) + end + + it 'should print error and exit when given only 1 argument and Chef::REST throws an exception' do + @knife.name_args = ['cookbook_name'] + @noauth_rest.should_receive(:get_rest).with("http://cookbooks.opscode.com/api/v1/cookbooks/cookbook_name") { raise Errno::ECONNREFUSED, "Connection refused" } @knife.ui.should_receive(:fatal) lambda { @knife.run }.should raise_error(SystemExit) end |