Steal the run method back from Thor so that it can be used in place for exec for running commands in the foreman environment.

Fix some error reporting.
This commit is contained in:
Matt Griffin
2012-01-09 17:11:32 -05:00
parent a34bc59721
commit 9432989fbe
2 changed files with 25 additions and 18 deletions
+17 -10
View File
@@ -5,15 +5,21 @@ require "thor"
require "yaml"
class Foreman::CLI < Thor
class_option :procfile, :type => :string, :aliases => "-f", :desc => "Default: Procfile"
desc "start", "Start the application"
method_option :env, :type => :string, :aliases => "-e", :desc => "Specify an environment file to load, defaults to .env"
method_option :port, :type => :numeric, :aliases => "-p"
method_option :concurrency, :type => :string, :aliases => "-c", :banner => '"alpha=5,bar=3"'
class << self
# Hackery. Take the run method away from Thor so that we can redefine it.
def is_thor_reserved_word?(word, type)
return false if word == 'run'
super
end
end
def start
check_procfile!
engine.start
@@ -54,15 +60,17 @@ class Foreman::CLI < Thor
display "valid procfile detected (#{engine.procfile.process_names.join(', ')})"
end
desc "exec COMMAND", "Run a command using your application's environment"
desc "run COMMAND", "Run a command using your application's environment"
def exec(*args)
def run(*args)
engine.apply_environment!
Kernel.exec args.join(" ")
rescue Errno::EACCES
error "not executable: #{args.first}"
rescue Errno::ENOENT
error "command not found: #{args.first}"
begin
exec args.join(" ")
rescue Errno::EACCES
error "not executable: #{args.first}"
rescue Errno::ENOENT
error "command not found: #{args.first}"
end
end
private ######################################################################
@@ -98,5 +106,4 @@ private ######################################################################
defaults = YAML::load_file(".foreman") || {}
Thor::CoreExt::HashWithIndifferentAccess.new(defaults.merge(original_options))
end
end
+8 -8
View File
@@ -90,7 +90,7 @@ describe "Foreman::CLI" do
end
end
describe "exec" do
describe "run" do
describe "with a valid Procfile" do
before { write_procfile }
@@ -98,22 +98,22 @@ describe "Foreman::CLI" do
let(:command) { ["ls", "-l"] }
before(:each) do
stub(Kernel).exec
stub(subject).exec
end
it "should load the environment file" do
write_env
preserving_env do
subject.exec *command
subject.run *command
ENV["FOO"].should == "bar"
end
ENV["FOO"].should be_nil
end
it "should execute the command as a string" do
mock(Kernel).exec(command.join(" "))
subject.exec *command
it "should runute the command as a string" do
mock(subject).exec(command.join(" "))
subject.run *command
end
end
@@ -122,7 +122,7 @@ describe "Foreman::CLI" do
it "should print an error" do
mock_error(subject, "command not found: #{command}") do
subject.exec command
subject.run command
end
end
end
@@ -132,7 +132,7 @@ describe "Foreman::CLI" do
it "should print an error" do
mock_error(subject, "not executable: #{command}") do
subject.exec command
subject.run command
end
end
end