Add --template option to init command, with graceful fallback to default Vagrantfile template behavior

This commit is contained in:
Zachary Flower 2017-11-17 11:30:10 -07:00
parent bcf3a4b3d1
commit 14d596f08e
1 changed files with 21 additions and 6 deletions

View File

@ -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