Merge pull request #121 from Viximo/feature/run
Add "exec" action to allow execution of commands within the app environment
This commit is contained in:
commit
98337c92e1
4 files changed
+98
-1
No files matched your search
+21
-1
@@ -14,6 +14,14 @@ class Foreman::CLI < Thor
|
|||||||
method_option :port, :type => :numeric, :aliases => "-p"
|
method_option :port, :type => :numeric, :aliases => "-p"
|
||||||
method_option :concurrency, :type => :string, :aliases => "-c", :banner => '"alpha=5,bar=3"'
|
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
|
def start
|
||||||
check_procfile!
|
check_procfile!
|
||||||
engine.start
|
engine.start
|
||||||
@@ -54,6 +62,19 @@ class Foreman::CLI < Thor
|
|||||||
display "valid procfile detected (#{engine.procfile.process_names.join(', ')})"
|
display "valid procfile detected (#{engine.procfile.process_names.join(', ')})"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
desc "run COMMAND", "Run a command using your application's environment"
|
||||||
|
|
||||||
|
def run(*args)
|
||||||
|
engine.apply_environment!
|
||||||
|
begin
|
||||||
|
exec args.join(" ")
|
||||||
|
rescue Errno::EACCES
|
||||||
|
error "not executable: #{args.first}"
|
||||||
|
rescue Errno::ENOENT
|
||||||
|
error "command not found: #{args.first}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private ######################################################################
|
private ######################################################################
|
||||||
|
|
||||||
def check_procfile!
|
def check_procfile!
|
||||||
@@ -87,5 +108,4 @@ private ######################################################################
|
|||||||
defaults = YAML::load_file(".foreman") || {}
|
defaults = YAML::load_file(".foreman") || {}
|
||||||
Thor::CoreExt::HashWithIndifferentAccess.new(defaults.merge(original_options))
|
Thor::CoreExt::HashWithIndifferentAccess.new(defaults.merge(original_options))
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -90,4 +90,53 @@ describe "Foreman::CLI" do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "run" do
|
||||||
|
describe "with a valid Procfile" do
|
||||||
|
before { write_procfile }
|
||||||
|
|
||||||
|
describe "and a command" do
|
||||||
|
let(:command) { ["ls", "-l"] }
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
stub(subject).exec
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should load the environment file" do
|
||||||
|
write_env
|
||||||
|
preserving_env do
|
||||||
|
subject.run *command
|
||||||
|
ENV["FOO"].should == "bar"
|
||||||
|
end
|
||||||
|
|
||||||
|
ENV["FOO"].should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should runute the command as a string" do
|
||||||
|
mock(subject).exec(command.join(" "))
|
||||||
|
subject.run *command
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "and a non-existent command" do
|
||||||
|
let(:command) { "iuhtngrglhulhdfg" }
|
||||||
|
|
||||||
|
it "should print an error" do
|
||||||
|
mock_error(subject, "command not found: #{command}") do
|
||||||
|
subject.run command
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "and a non-executable command" do
|
||||||
|
let(:command) { __FILE__ }
|
||||||
|
|
||||||
|
it "should print an error" do
|
||||||
|
mock_error(subject, "not executable: #{command}") do
|
||||||
|
subject.run command
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe "spec helpers" do
|
||||||
|
describe "#preserving_env" do
|
||||||
|
after { ENV.delete "FOO" }
|
||||||
|
|
||||||
|
it "should remove added environment vars" do
|
||||||
|
preserving_env { ENV["FOO"] = "baz" }
|
||||||
|
ENV["FOO"].should == nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should reset modified environment vars" do
|
||||||
|
ENV["FOO"] = "bar"
|
||||||
|
preserving_env { ENV["FOO"] = "baz"}
|
||||||
|
ENV["FOO"].should == "bar"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -63,6 +63,16 @@ def example_export_file(filename)
|
|||||||
data
|
data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def preserving_env
|
||||||
|
old_env = ENV.to_hash
|
||||||
|
begin
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
ENV.clear
|
||||||
|
ENV.update(old_env)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
config.color_enabled = true
|
config.color_enabled = true
|
||||||
config.include FakeFS::SpecHelpers
|
config.include FakeFS::SpecHelpers
|
||||||
|
|||||||
Reference in new issue
Block a user