diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 990b77282..166c0dfb3 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -97,6 +97,7 @@ I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_ro Vagrant.commands.register(:box) { Vagrant::Command::Box } Vagrant.commands.register(:destroy) { Vagrant::Command::Destroy } Vagrant.commands.register(:halt) { Vagrant::Command::Halt } +Vagrant.commands.register(:init) { Vagrant::Command::Init } Vagrant.commands.register(:package) { Vagrant::Command::Package } Vagrant.commands.register(:provision) { Vagrant::Command::Provision } Vagrant.commands.register(:reload) { Vagrant::Command::Reload } diff --git a/lib/vagrant/command.rb b/lib/vagrant/command.rb index 2c6f6da26..2524a1646 100644 --- a/lib/vagrant/command.rb +++ b/lib/vagrant/command.rb @@ -9,6 +9,7 @@ module Vagrant autoload :BoxList, 'vagrant/command/box_list' autoload :Destroy, 'vagrant/command/destroy' autoload :Halt, 'vagrant/command/halt' + autoload :Init, 'vagrant/command/init' autoload :Package, 'vagrant/command/package' autoload :Provision, 'vagrant/command/provision' autoload :Reload, 'vagrant/command/reload' diff --git a/lib/vagrant/command/init.rb b/lib/vagrant/command/init.rb index a6fd4cfad..f1f18adb6 100644 --- a/lib/vagrant/command/init.rb +++ b/lib/vagrant/command/init.rb @@ -1,13 +1,36 @@ +require 'optparse' + +require 'vagrant/util/template_renderer' + module Vagrant module Command - class InitCommand < Base - argument :box_name, :type => :string, :optional => true, :default => "base" - argument :box_url, :type => :string, :optional => true - source_root File.expand_path("templates/commands/init", Vagrant.source_root) - register "init [box_name] [box_url]", "Initializes the current folder for Vagrant usage" - + class Init < Base def execute - template "Vagrantfile.erb", env.cwd.join("Vagrantfile") + options = {} + + opts = OptionParser.new do |opts| + opts.banner = "Usage: vagrant init [box-name] [box-url]" + end + + # Parse the options + argv = parse_options(opts) + return if !argv + + save_path = @env.cwd.join("Vagrantfile") + raise Errors::VagrantfileExistsError if save_path.exist? + + template_path = ::Vagrant.source_root.join("templates/commands/init/Vagrantfile") + contents = Vagrant::Util::TemplateRenderer.render(template_path, + :box_name => argv[0] || "base", + :box_url => argv[1]) + + # Write out the contents + save_path.open("w+") do |f| + f.write(contents) + end + + @env.ui.info(I18n.t("vagrant.commands.init.success"), + :prefix => false) end end end diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 8f1534c9d..0f8ff75a9 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -290,6 +290,11 @@ module Vagrant error_key(:interrupted) end + class VagrantfileExistsError < VagrantError + status_code(58) + error_key(:vagrantfile_exists) + end + class VagrantfileSyntaxError < VagrantError status_code(41) error_key(:vagrantfile_syntax_error) diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 877dd30d2..0c611fdb1 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -115,6 +115,9 @@ en: http://vagrantup.com/docs/getting-started/setup/windows.html + vagrantfile_exists: |- + `Vagrantfile` already exists in this directory. Remove it before + running `vagrant init`. vagrantfile_syntax_error: |- There is a syntax error in the following Vagrantfile. The syntax error message is reproduced below for convenience: @@ -197,6 +200,12 @@ en: vm_not_running: "VM is not currently running. Please bring it up to run this command." box: no_installed_boxes: "There are no installed boxes! Use `vagrant box add` to add some." + init: + success: |- + A `Vagrantfile` has been placed in this directory. You are now + ready to `vagrant up` your first virtual environment! Please read + the comments in the Vagrantfile as well as documentation on + `vagrantup.com` for more information on using Vagrant. status: aborted: |- The VM is in an aborted state. This means that it was abruptly