diff --git a/lib/vagrant/action/general/package.rb b/lib/vagrant/action/general/package.rb index c969486a2..a2a4e5c8c 100644 --- a/lib/vagrant/action/general/package.rb +++ b/lib/vagrant/action/general/package.rb @@ -73,6 +73,8 @@ module Vagrant self.class.validate!(env["package.output"], env["package.directory"]) + package_with_folder_path if env["package.output"].include?(File::SEPARATOR) + raise Errors::PackageOutputDirectory if File.directory?(fullpath) @app.call(env) @@ -83,6 +85,16 @@ module Vagrant compress end + def package_with_folder_path + folder_path = File.expand_path("..", @fullpath) + create_box_folder(folder_path) unless File.directory?(folder_path) + end + + def create_box_folder(folder_path) + @env[:ui].info(I18n.t("vagrant.actions.general.package.box_folder", folder_path: folder_path)) + FileUtils.mkdir_p(folder_path) + end + def recover(env) @env = env diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 8136fb86f..0e673586a 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -2117,6 +2117,7 @@ en: package: packaging: "Packaging additional file: %{file}" compressing: "Compressing package to: %{fullpath}" + box_folder: "Creating new folder: %{folder_path}" output_exists: |- The specified file '%{filename}' to save the package as already exists. Please remove this file or specify a different file name for outputting. diff --git a/test/unit/plugins/commands/package/command_test.rb b/test/unit/plugins/commands/package/command_test.rb index 7888c8860..49a5fbe89 100644 --- a/test/unit/plugins/commands/package/command_test.rb +++ b/test/unit/plugins/commands/package/command_test.rb @@ -57,6 +57,18 @@ describe VagrantPlugins::CommandPackage::Command do expect{ package_command.execute }.to raise_error(Vagrant::Errors::MachineNotFound) end end + + context "with --output option" do + + let(:argv){ ['--output', 'package-output-folder/default'] } + + it "packages default machine inside specified folder" do + expect(package_command).to receive(:package_vm).with( + a_machine_named('default'), :output => "package-output-folder/default" + ) + package_command.execute + end + end end context "with multiple arguments" do