Skip to content

Commit

Permalink
make sudo helper play nicely with complex commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis committed May 1, 2008
1 parent c09e810 commit f9d2af0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*unreleased*

* Make sudo helper play nicely with complex command chains [Jamis Buck]

* Expand file-transfer options with new upload() and download() helpers. [Jamis Buck]

* Allow SCP transfers in addition to SFTP. [Jamis Buck]
Expand Down
4 changes: 2 additions & 2 deletions lib/capistrano/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ def open_channels
if options[:shell] == false
shell = nil
else
shell = "#{options[:shell] || "sh"} -c"
shell = [options.fetch(:shell, "sh"), "-c"].join(" ")
cmd = cmd.gsub(/[$\\`"]/) { |m| "\\#{m}" }
cmd = "\"#{cmd}\""
end

command_line = [environment, shell, cmd].compact.join(" ")
command_line = [environment, options[:command_prefix], shell, cmd].compact.join(" ")

ch.exec(command_line)
ch.send_data(options[:data]) if options[:data]
Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano/configuration/actions/invocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def sudo(command, options={}, &block)
as = options.delete(:as)

user = as && "-u #{as}"
command = [fetch(:sudo, "sudo"), "-p '#{sudo_prompt}'", user, command].compact.join(" ")
options[:command_prefix] = [fetch(:sudo, "sudo"), "-p '#{sudo_prompt}'", user].compact.join(" ")

run(command, options, &sudo_behavior_callback(block))
end
Expand Down
10 changes: 5 additions & 5 deletions test/configuration/actions/invocation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,29 @@ def test_default_io_proc_should_log_stderr_arguments_as_important
end

def test_sudo_should_default_to_sudo
@config.expects(:run).with("sudo -p 'sudo password: ' ls", {})
@config.expects(:run).with("ls", :command_prefix => "sudo -p 'sudo password: '")
@config.sudo "ls"
end

def test_sudo_should_use_sudo_variable_definition
@config.expects(:run).with("/opt/local/bin/sudo -p 'sudo password: ' ls", {})
@config.expects(:run).with("ls", :command_prefix => "/opt/local/bin/sudo -p 'sudo password: '")
@config.options[:sudo] = "/opt/local/bin/sudo"
@config.sudo "ls"
end

def test_sudo_should_interpret_as_option_as_user
@config.expects(:run).with("sudo -p 'sudo password: ' -u app ls", {})
@config.expects(:run).with("ls", :command_prefix => "sudo -p 'sudo password: ' -u app")
@config.sudo "ls", :as => "app"
end

def test_sudo_should_pass_options_through_to_run
@config.expects(:run).with("sudo -p 'sudo password: ' ls", :foo => "bar")
@config.expects(:run).with("ls", :command_prefix => "sudo -p 'sudo password: '", :foo => "bar")
@config.sudo "ls", :foo => "bar"
end

def test_sudo_should_interpret_sudo_prompt_variable_as_custom_prompt
@config.set :sudo_prompt, "give it to me: "
@config.expects(:run).with("sudo -p 'give it to me: ' ls", {})
@config.expects(:run).with("ls", :command_prefix => "sudo -p 'give it to me: '")
@config.sudo "ls"
end

Expand Down

0 comments on commit f9d2af0

Please sign in to comment.