Skip to content

Commit

Permalink
Box#bash takes an :env option with a hash of environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Wiggins committed Mar 23, 2008
1 parent d0fc256 commit b64e06b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/rush/box.rb
Expand Up @@ -49,13 +49,27 @@ def processes
end
end

# Execute a command in the standard unix shell. Optional parameter :user
# switches to that user via sudo first, if you have permission. Example:
# Execute a command in the standard unix shell. Options:
#
# :user => unix username to become via sudo
# :env => hash of environment variables
#
# Example:
#
# box.bash '/etc/init.d/mysql restart', :user => 'root'
# box.bash 'rake db:migrate', :user => 'www', :env => { :RAILS_ENV => 'production' }
#
def bash(command, options={})
connection.bash(command, options[:user])
connection.bash(command_with_environment(command, options[:env]), options[:user])
end

def command_with_environment(command, env) # :nodoc:
return command unless env

vars = env.map do |key, value|
"export #{key}='#{value}'"
end
vars.push(command).join("\n")
end

# Returns true if the box is responding to commands.
Expand Down
10 changes: 10 additions & 0 deletions spec/box_spec.rb
Expand Up @@ -31,6 +31,16 @@
@box.bash('cmd', :user => 'user')
end

it "builds a script of environment variables to prefix the bash command" do
@box.command_with_environment('cmd', { :a => 'b' }).should == "export a='b'\ncmd"
end

it "sets the environment variables from the provided hash" do
@box.connection.stub!(:bash)
@box.should_receive(:command_with_environment).with('cmd', { 1 => 2 })
@box.bash('cmd', :env => { 1 => 2 })
end

it "checks the connection to determine if it is alive" do
@box.connection.should_receive(:alive?).and_return(true)
@box.should be_alive
Expand Down

0 comments on commit b64e06b

Please sign in to comment.