Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #16 from motns/working_directory_fix
Browse files Browse the repository at this point in the history
pull request for issue #15
  • Loading branch information
Seth Vargo committed Apr 3, 2013
2 parents c1b8edf + 02d6cb5 commit 8389efd
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/strainer/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def initialize(line, cookbook, options = {})
def run!
title(label)

Strainer.ui.debug "Changing working directory to '#{Strainer.sandbox_path}'"
Dir.chdir Strainer.sandbox_path do
inside_sandbox do
Strainer.ui.debug "Running '#{command}'"
speak command
PTY.spawn command do |r, _, pid|
Expand All @@ -63,6 +62,23 @@ def run!
end
end

# Execute a block inside the sandbox directory defined in 'Strainer.sandbox_path'.
# This will first change the 'PWD' env variable to the sandbox path, and then
# pass the given block into 'Dir.chdir'. 'PWD' is restored to the original value
# when the block is finished.
#
# @yield The block to execute inside the sandbox
def inside_sandbox(&block)
Strainer.ui.debug "Changing working directory to '#{Strainer.sandbox_path}'"
original_pwd = ENV['PWD']

ENV['PWD'] = Strainer.sandbox_path.to_s
Dir.chdir(Strainer.sandbox_path, &block)
ENV['PWD'] = original_pwd

Strainer.ui.debug "Restored working directory to '#{original_pwd}'"
end

# Have this command output text, prefixing with its output with the
# command name
#
Expand Down

0 comments on commit 8389efd

Please sign in to comment.