`vagrant init` uses new OptParse based CLI

This commit is contained in:
Mitchell Hashimoto 2011-12-17 21:06:00 -08:00
parent 1770ad1ee5
commit 98df762f8c
5 changed files with 46 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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

View File

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