diff --git a/lib/vagrant/action/builtin/box_add.rb b/lib/vagrant/action/builtin/box_add.rb index 513cf6886..138f81c75 100644 --- a/lib/vagrant/action/builtin/box_add.rb +++ b/lib/vagrant/action/builtin/box_add.rb @@ -101,6 +101,28 @@ module Vagrant # provider. metadata_provider = metadata_version.provider( metadata_version.providers.first) + else + providers = metadata_version.providers.sort + + choice = 0 + options = providers.map do |p| + choice += 1 + "#{choice}) #{p}" + end.join("\n") + + # We have more than one provider, ask the user what they want + choice = env[:ui].ask(I18n.t( + "vagrant.box_add_choose_provider", + options: options)) + choice = choice.to_i if choice + while !choice || choice <= 0 || choice > providers.length + choice = env[:ui].ask(I18n.t( + "vagrant.box_add_choose_provider_again")) + choice = choice.to_i if choice + end + + metadata_provider = metadata_version.provider( + providers[choice-1]) end box_add( @@ -331,7 +353,6 @@ module Vagrant end end - # TODO: do the HEAD request output = d.head match = output.scan(/^Content-Type: (.+?)$/).last return false if !match diff --git a/templates/locales/en.yml b/templates/locales/en.yml index d42bfbbf1..4dd04d08f 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -4,6 +4,16 @@ en: Machine booted and ready! boot_waiting: |- Waiting for machine to boot. This may take a few minutes... + box_add_choose_provider: |- + This box can work with multiple providers! The providers that it + can work with are listed below. Please review the list and choose + the provider you will be working with. + + %{options} + + Enter your choice: + box_add_choose_provider_again: |- + Invalid choice. Try again: box_add_with_version: |- Adding box '%{name}' (v%{version}) for '%{provider}' provider... box_added: |- diff --git a/test/unit/vagrant/action/builtin/box_add_test.rb b/test/unit/vagrant/action/builtin/box_add_test.rb index a2391132c..0940bd0b7 100644 --- a/test/unit/vagrant/action/builtin/box_add_test.rb +++ b/test/unit/vagrant/action/builtin/box_add_test.rb @@ -450,6 +450,51 @@ describe Vagrant::Action::Builtin::BoxAdd do expect(env[:box_added]).to equal(box) end + it "asks the user what provider if multiple options" do + box_path = iso_env.box2_file(:virtualbox) + tf = Tempfile.new("vagrant").tap do |f| + f.write(<<-RAW) + { + "name": "foo/bar", + "versions": [ + { + "version": "0.5" + }, + { + "version": "0.7", + "providers": [ + { + "name": "virtualbox", + "url": "#{box_path}" + }, + { + "name": "vmware", + "url": "#{iso_env.box2_file(:vmware)}" + } + ] + } + ] + } + RAW + f.close + end + + env[:box_url] = tf.path + + env[:ui].should_receive(:ask).and_return("1") + + box_collection.should_receive(:add).with do |path, name, version| + expect(checksum(path)).to eq(checksum(box_path)) + expect(name).to eq("foo/bar") + expect(version).to eq("0.7") + true + end.and_return(box) + + app.should_receive(:call).with(env) + + subject.call(env) + end + it "raises an exception if no matching version" do box_path = iso_env.box2_file(:vmware) tf = Tempfile.new("vagrant").tap do |f|