Began work on new CLI.

This commit is contained in:
Mitchell Hashimoto 2010-08-23 23:44:42 -07:00
parent b21bfbf84a
commit 5e42f8bbb2
6 changed files with 94 additions and 4 deletions

View File

@ -1,7 +1,5 @@
#!/usr/bin/env ruby
# Get library
libdir = File.join(File.dirname(__FILE__), '..', 'lib')
require File.expand_path('vagrant', libdir)
require 'vagrant'
# Call the command
Vagrant::Command.execute(*ARGV)
Vagrant::Command.execute(*ARGV)

16
bin/vagrant-thor Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env ruby
# NOTE: This file is TEMPORARY. Eventually the plan is for this binary
# to become the new `vagrant` binary. The only reason for the separation
# now is so that both can be tested in parallel.
require 'vagrant'
require 'vagrant/cli'
begin
Vagrant::CLI.start(ARGV, :env => Vagrant::Environment.load!)
rescue Vagrant::VagrantError => e
Vagrant.ui.error e.message
Vagrant.ui.error e.backtrace.join("\n")
exit e.status_code
end

View File

@ -4,12 +4,28 @@ require "vagrant/util/glob_loader"
module Vagrant
class << self
attr_writer :ui
# The source root is the path to the root directory of
# the Vagrant gem.
def source_root
@source_root ||= File.expand_path('../../', __FILE__)
end
# Returns the {UI} class to use for talking with the
# outside world.
def ui
@ui ||= UI.new
end
end
class VagrantError < StandardError
def self.status_code(code = nil)
define_method(:status_code) { code }
end
end
class CLIMissingEnvironment < VagrantError; status_code(1); end
end
# Load them up. One day we'll convert this to autoloads. Today

19
lib/vagrant/cli.rb Normal file
View File

@ -0,0 +1,19 @@
require 'thor'
module Vagrant
class CLI < Thor
attr_reader :env
def initialize(args, options, config)
super
# Set the UI to a shell based UI using the shell object which
# Thor sets up.
Vagrant.ui = UI::Shell.new(shell)
# The last argument must _always_ be a Vagrant Environment class.
raise CLIMissingEnvironment.new("This command requires that a Vagrant environment be properly passed in as the last parameter.") if !config[:env]
@env = config[:env]
end
end
end

View File

@ -129,6 +129,12 @@ module Vagrant
end
end
# Makes a call to the CLI with the given arguments as if they
# came from the real command line (sometimes they do!)
def cli(*args)
Vagrant::CLI.start(args.flatten, :env => self)
end
#---------------------------------------------------------------
# Load Methods
#---------------------------------------------------------------

35
lib/vagrant/ui.rb Normal file
View File

@ -0,0 +1,35 @@
module Vagrant
# Vagrant UIs handle communication with the outside world (typically
# through a shell). They must respond to the typically logger methods
# of `warn`, `error`, `info`, and `confirm`.
class UI
[:warn, :error, :info, :confirm].each do |method|
# By default these methods don't do anything. A silent UI.
define_method(method) { |message| }
end
# A shell UI, which uses a `Thor::Shell` object to talk with
# a terminal.
class Shell < UI
def initialize(shell)
@shell = shell
end
def warn(message)
@shell.say(message, :yellow)
end
def error(message)
@shell.say(message, :red)
end
def info(message)
@shell.say(message)
end
def confirm(message)
@shell.say(message, :green)
end
end
end
end