Skip to content

Commit

Permalink
avoid duplicate calls to env methods
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 7, 2019
1 parent 5fa26ba commit 5431690
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ appear at the top.
## [Unreleased][]

* Your contribution here!
* [#453](https://github.com/capistrano/sshkit/pull/453): fix and unify shell escaping for user/group/directory - [@grosser](https://github.com/grosser)

## [1.18.2][] (2019-02-03)

Expand Down
14 changes: 8 additions & 6 deletions lib/sshkit/backends/abstract.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'shellwords'

module SSHKit

module Backend
Expand Down Expand Up @@ -81,12 +83,12 @@ def execute(*args)
def within(directory, &_block)
(@pwd ||= []).push directory.to_s
execute <<-EOTEST, verbosity: Logger::DEBUG
if test ! -d #{File.join(@pwd)}
then echo "Directory does not exist '#{File.join(@pwd)}'" 1>&2
if test ! -d #{File.join(@pwd).shellescape}
then echo "Directory does not exist '#{File.join(@pwd).shellescape}'" 1>&2
false
fi
EOTEST
yield
EOTEST
yield
ensure
@pwd.pop
end
Expand All @@ -108,8 +110,8 @@ def as(who, &_block)
@group = nil
end
execute <<-EOTEST, verbosity: Logger::DEBUG
if ! sudo -u #{@user} whoami > /dev/null
then echo "You cannot switch to user '#{@user}' using sudo, please check the sudoers file" 1>&2
if ! sudo -u #{@user.to_s.shellescape} whoami > /dev/null
then echo "You cannot switch to user '#{@user.to_s.shellescape}' using sudo, please check the sudoers file" 1>&2
false
fi
EOTEST
Expand Down
8 changes: 5 additions & 3 deletions lib/sshkit/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,15 @@ def environment_string
end

def with(&_block)
return yield unless environment_hash.any?
"( export #{environment_string} ; #{yield} )"
env_string = environment_string
return yield if env_string.empty?
"( export #{env_string} ; #{yield} )"
end

def user(&_block)
return yield unless options[:user]
"sudo -u #{options[:user].to_s.shellescape} #{environment_string + " " unless environment_string.empty?}-- sh -c #{yield.shellescape}"
env_string = environment_string
"sudo -u #{options[:user].to_s.shellescape} #{env_string + " " unless env_string.empty?}-- sh -c #{yield.shellescape}"
end

def in_background(&_block)
Expand Down

0 comments on commit 5431690

Please sign in to comment.