From 14d596f08e59188a76f57d868ddcc08f988d43ee Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Fri, 17 Nov 2017 11:30:10 -0700 Subject: [PATCH] Add --template option to init command, with graceful fallback to default Vagrantfile template behavior --- plugins/commands/init/command.rb | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/plugins/commands/init/command.rb b/plugins/commands/init/command.rb index 91bd0ff8b..e28a58c74 100644 --- a/plugins/commands/init/command.rb +++ b/plugins/commands/init/command.rb @@ -14,6 +14,7 @@ module VagrantPlugins force: false, minimal: false, output: "Vagrantfile", + template: nil } opts = OptionParser.new do |o| @@ -30,7 +31,7 @@ module VagrantPlugins options[:force] = f end - o.on("-m", "--minimal", "Create minimal Vagrantfile (no help comments)") do |m| + o.on("-m", "--minimal", "Use minimal Vagrantfile template (no help comments). Ignored with --template") do |m| options[:minimal] = m end @@ -38,6 +39,10 @@ module VagrantPlugins "Output path for the box. '-' for stdout") do |output| options[:output] = output end + + o.on("--template FILE", String, "Path to Vagrantfile template") do |template| + options[:template] = template + end end # Parse the options @@ -51,16 +56,26 @@ module VagrantPlugins raise Vagrant::Errors::VagrantfileExistsError if save_path.exist? end - template = "templates/commands/init/Vagrantfile" - if options[:minimal] - template = "templates/commands/init/Vagrantfile.min" + # Determine the template and template root to use + template_root = "" + if options[:template].nil? + options[:template] = "Vagrantfile" + + if options[:minimal] + options[:template] = "Vagrantfile.min" + end + + template_root = ::Vagrant.source_root.join("templates/commands/init") end - template_path = ::Vagrant.source_root.join(template) - contents = Vagrant::Util::TemplateRenderer.render(template_path, + # Strip the .erb extension off the template if the user passes it in + options[:template] = options[:template].chomp(".erb") + + contents = Vagrant::Util::TemplateRenderer.render(options[:template], box_name: argv[0] || "base", box_url: argv[1], box_version: options[:box_version], + template_root: template_root ) if save_path