Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Close input streams when sending commands that don't read input.

An :eof option is added to the run method so that this can be specified
explicitly where a block is provided that may or may not need to read from
the input stream.
  • Loading branch information...
commit 784c98a640e63d020cbf33d8b25ef5ce7eadd6be 1 parent dfb6295
@dylanahsmith dylanahsmith authored
View
1  lib/capistrano/command.rb
@@ -221,6 +221,7 @@ def open_channels
ch.exec(command_line)
ch.send_data(options[:data]) if options[:data]
+ ch.eof! if options[:eof]
else
# just log it, don't actually raise an exception, since the
# process method will see that the status is not zero and will
View
4 lib/capistrano/configuration/actions/inspect.rb
@@ -20,7 +20,7 @@ module Inspect
# stream "tail -f #{shared_path}/log/fastcgi.crash.log"
# end
def stream(command, options={})
- invoke_command(command, options) do |ch, stream, out|
+ invoke_command(command, options.merge(:eof => true)) do |ch, stream, out|
puts out if stream == :out
warn "[err :: #{ch[:server]}] #{out}" if stream == :err
end
@@ -31,7 +31,7 @@ def stream(command, options={})
# string. The command is invoked via #invoke_command.
def capture(command, options={})
output = ""
- invoke_command(command, options.merge(:once => true)) do |ch, stream, data|
+ invoke_command(command, options.merge(:once => true, :eof => true)) do |ch, stream, data|
case stream
when :out then output << data
when :err then warn "[err :: #{ch[:server]}] #{data}"
View
5 lib/capistrano/configuration/actions/invocation.rb
@@ -138,11 +138,16 @@ def invoke_command(cmd, options={}, &block)
# and the values should be their corresponding values. The default is
# empty, but may be modified by changing the +default_environment+
# Capistrano variable.
+ # * :eof - if true, the standard input stream will be closed after sending
+ # any data specified in the :data option. If false, the input stream is
+ # left open. The default is to close the input stream only if no block is
+ # passed.
#
# Note that if you set these keys in the +default_run_options+ Capistrano
# variable, they will apply for all invocations of #run, #invoke_command,
# and #parallel.
def run(cmd, options={}, &block)
+ options = options.merge(:eof => !block_given?) if options[:eof].nil?
block ||= self.class.default_io_proc
tree = Command::Tree.new(self) { |t| t.else(cmd, &block) }
run_tree(tree, options)
View
8 test/command_test.rb
@@ -254,6 +254,14 @@ def test_process_with_unknown_placeholder_should_not_replace_placeholder
Capistrano::Command.new("echo $CAPISTRANO:OTHER$", [session])
end
+ def test_input_stream_closed_when_eof_option_is_true
+ channel = nil
+ session = setup_for_extracting_channel_action { |ch| channel = ch }
+ channel.expects(:eof!)
+ Capistrano::Command.new("cat", [session], :data => "here we go", :eof => true)
+ assert_equal({ :data => 'here we go', :eof => true }, channel[:options])
+ end
+
private
def mock_session(channel=nil)
View
4 test/configuration/actions/inspect_test.rb
@@ -12,7 +12,7 @@ def setup
end
def test_stream_should_pass_options_through_to_run
- @config.expects(:invoke_command).with("tail -f foo.log", :once => true)
+ @config.expects(:invoke_command).with("tail -f foo.log", :once => true, :eof => true)
@config.stream("tail -f foo.log", :once => true)
end
@@ -33,7 +33,7 @@ def test_stream_should_emit_stderr_via_warn
end
def test_capture_should_pass_options_merged_with_once_to_run
- @config.expects(:invoke_command).with("hostname", :foo => "bar", :once => true)
+ @config.expects(:invoke_command).with("hostname", :foo => "bar", :once => true, :eof => true)
@config.capture("hostname", :foo => "bar")
end
View
2  test/configuration/actions/invocation_test.rb
@@ -45,7 +45,7 @@ def teardown
end
def test_run_options_should_be_passed_to_execute_on_servers
- @config.expects(:execute_on_servers).with(:foo => "bar")
+ @config.expects(:execute_on_servers).with(:foo => "bar", :eof => true)
@config.run "ls", :foo => "bar"
end
View
2  test/configuration_test.rb
@@ -16,7 +16,7 @@ def test_connections_execution_loading_namespaces_roles_and_variables_modules_sh
process_args = Proc.new do |tree, session, opts|
tree.fallback.command == "echo 'hello world'" &&
session == [:session] &&
- opts == { :logger => @config.logger }
+ opts == { :logger => @config.logger, :eof => true }
end
Capistrano::Command.expects(:process).with(&process_args)
Please sign in to comment.
Something went wrong with that request. Please try again.