2011-12-18 05:06:00 +00:00
|
|
|
require 'optparse'
|
|
|
|
|
|
|
|
require 'vagrant/util/template_renderer'
|
|
|
|
|
2012-04-19 20:59:48 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module CommandInit
|
2012-11-07 05:05:14 +00:00
|
|
|
class Command < Vagrant.plugin("2", :command)
|
2013-11-23 22:00:42 +00:00
|
|
|
def self.synopsis
|
|
|
|
"initializes a new Vagrant environment by creating a Vagrantfile"
|
|
|
|
end
|
2011-12-18 05:06:00 +00:00
|
|
|
|
2013-11-23 22:00:42 +00:00
|
|
|
def execute
|
2014-04-29 03:22:09 +00:00
|
|
|
options = {
|
|
|
|
force: false,
|
2014-05-05 00:36:51 +00:00
|
|
|
minimal: false,
|
2014-04-29 03:22:09 +00:00
|
|
|
output: "Vagrantfile",
|
|
|
|
}
|
2013-11-24 19:53:08 +00:00
|
|
|
|
2013-11-23 22:00:42 +00:00
|
|
|
opts = OptionParser.new do |o|
|
2014-02-08 08:20:50 +00:00
|
|
|
o.banner = "Usage: vagrant init [name] [url]"
|
|
|
|
o.separator ""
|
|
|
|
o.separator "Options:"
|
|
|
|
o.separator ""
|
2013-11-24 19:53:08 +00:00
|
|
|
|
2014-04-29 03:22:09 +00:00
|
|
|
o.on("-f", "--force", "Overwrite existing Vagrantfile") do |f|
|
|
|
|
options[:force] = f
|
|
|
|
end
|
|
|
|
|
2014-05-05 00:36:51 +00:00
|
|
|
o.on("-m", "--minimal", "Create minimal Vagrantfile (no help comments)") do |m|
|
|
|
|
options[:minimal] = m
|
|
|
|
end
|
|
|
|
|
2014-02-08 08:20:50 +00:00
|
|
|
o.on("--output FILE", String,
|
|
|
|
"Output path for the box. '-' for stdout") do |output|
|
2013-11-24 19:53:08 +00:00
|
|
|
options[:output] = output
|
|
|
|
end
|
2011-12-18 05:06:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Parse the options
|
|
|
|
argv = parse_options(opts)
|
|
|
|
return if !argv
|
|
|
|
|
2013-11-24 19:53:08 +00:00
|
|
|
save_path = nil
|
|
|
|
if options[:output] != "-"
|
|
|
|
save_path = Pathname.new(options[:output]).expand_path(@env.cwd)
|
2014-04-29 03:22:09 +00:00
|
|
|
save_file.delete if save_path.exist? && options[:force]
|
2013-11-24 19:53:08 +00:00
|
|
|
raise Vagrant::Errors::VagrantfileExistsError if save_path.exist?
|
|
|
|
end
|
2011-12-18 05:06:00 +00:00
|
|
|
|
2014-05-05 00:36:51 +00:00
|
|
|
template = "templates/commands/init/Vagrantfile"
|
|
|
|
if options[:minimal]
|
|
|
|
template = "templates/commands/init/Vagrantfile.min"
|
|
|
|
end
|
|
|
|
|
|
|
|
template_path = ::Vagrant.source_root.join(template)
|
2011-12-18 05:06:00 +00:00
|
|
|
contents = Vagrant::Util::TemplateRenderer.render(template_path,
|
|
|
|
:box_name => argv[0] || "base",
|
|
|
|
:box_url => argv[1])
|
|
|
|
|
2013-11-24 19:53:08 +00:00
|
|
|
if save_path
|
|
|
|
# Write out the contents
|
2013-12-15 19:28:51 +00:00
|
|
|
begin
|
|
|
|
save_path.open("w+") do |f|
|
|
|
|
f.write(contents)
|
|
|
|
end
|
|
|
|
rescue Errno::EACCES
|
|
|
|
raise Vagrant::Errors::VagrantfileWriteError
|
2013-11-24 19:53:08 +00:00
|
|
|
end
|
2011-12-18 05:06:00 +00:00
|
|
|
|
2013-11-24 19:53:08 +00:00
|
|
|
@env.ui.info(I18n.t("vagrant.commands.init.success"), prefix: false)
|
|
|
|
else
|
|
|
|
@env.ui.info(contents, prefix: false)
|
|
|
|
end
|
2012-03-23 15:07:35 +00:00
|
|
|
|
|
|
|
# Success, exit status 0
|
|
|
|
0
|
2013-11-23 22:00:42 +00:00
|
|
|
end
|
2010-08-24 17:27:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|