diff --git a/lib/vagrant/commands/init.rb b/lib/vagrant/commands/init.rb index 62a2dc93b..acfeb9ff2 100644 --- a/lib/vagrant/commands/init.rb +++ b/lib/vagrant/commands/init.rb @@ -5,28 +5,37 @@ module Vagrant description "Initializes current folder for Vagrant usage" def execute(args) - create_vagrantfile(args[0]) + if args.empty? + create_vagrantfile + else + create_vagrantfile(:default_box => args[0] , :default_box_url => args[1]) + end + end def options_spec(opts) - opts.banner = "Usage: vagrant init [name]" + opts.banner = "Usage: vagrant init [name] [box_url]" end # Actually writes the initial Vagrantfile to the current working directory. # The Vagrantfile will contain the base box configuration specified, or # will just use "base" if none is specified. # - # @param [String] default_box The default base box for this Vagrantfile - def create_vagrantfile(default_box=nil) + # @param [String] :default_box The default base box for this + # Vagrantfile + # @param [String] :default_box_url The default url for fetching + # the given box for the Vagrantfile + def create_vagrantfile(opts={}) rootfile_path = File.join(Dir.pwd, Environment::ROOTFILE_NAME) error_and_exit(:rootfile_already_exists) if File.exist?(rootfile_path) # Write the rootfile - default_box ||= "base" + default_opts = { :default_box => "base", :default_box_url => nil}.merge(opts) + File.open(rootfile_path, 'w+') do |f| - f.write(TemplateRenderer.render(Environment::ROOTFILE_NAME, :default_box => default_box)) + f.write(TemplateRenderer.render(Environment::ROOTFILE_NAME, :default_box => default_opts[:default_box], :default_box_url => default_opts[:default_box_url])) end end end end -end \ No newline at end of file +end diff --git a/templates/Vagrantfile.erb b/templates/Vagrantfile.erb index 126f28526..68c1e3f12 100644 --- a/templates/Vagrantfile.erb +++ b/templates/Vagrantfile.erb @@ -5,4 +5,9 @@ Vagrant::Config.run do |config| # Every Vagrant virtual environment requires a box to build off of. config.vm.box = "<%= default_box %>" + + <% if !default_box_url.nil? %> + # The url from where the 'config.vm.box' box will be fetched + config.vm.box_url = "<%= default_box_url %>" + <% end %> end diff --git a/test/vagrant/commands/init_test.rb b/test/vagrant/commands/init_test.rb index 1772fb2e2..b04a65233 100644 --- a/test/vagrant/commands/init_test.rb +++ b/test/vagrant/commands/init_test.rb @@ -9,11 +9,21 @@ class CommandsInitTest < Test::Unit::TestCase end context "execute" do - should "create the vagrantfile with the first arg" do - args = [:foo] - @instance.expects(:create_vagrantfile).with(args.first) + should "create a vagrant file without any args" do + args = [] + @instance.expects(:create_vagrantfile).with(nil) @instance.execute(args) - end + end + context "when any arg is provided" do + should "create the vagrant file using the first arg as default_box and the second as default_box_url" do + args = [] + args[0] = "foo" + args[1] = "foo.box" + + @instance.expects(:create_vagrantfile).with(:default_box => "foo", :default_box_url => "foo.box") + @instance.execute(args) + end + end end context "creating the vagrantfile" do @@ -43,13 +53,21 @@ class CommandsInitTest < Test::Unit::TestCase should "use the given base box if given" do box = "zooo" - Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => box) - @instance.create_vagrantfile(box) + Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => box, :default_box_url => nil) + @instance.create_vagrantfile :default_box => box end + should "use the box_url if given" do + box_url = "fubar.box" + Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => "base", :default_box_url => "fubar.box") + @instance.create_vagrantfile :default_box_url => box_url + end + should "use the default `base` if no box is given" do - Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => "base") + Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => "base", :default_box_url => nil) @instance.create_vagrantfile end + + end end