6

In Capistrano 2.x you could capture the output line by line using

run "ls -la" do |channel, stream, data|
    puts data
end

This does not work in Capistrano 3.x, and the capture and execute commands do not seem to provide the same functionality.

Is there a way to replicate the 2.x behaviour in 3.x?

4

3 回答 3

14
output = capture('cat ~/file.cnf')
output.each_line do |line|
    puts line
end

这就是我使用捕获读取行的方式。如果你想在一行上捕捉特定的东西,你可以使用

if line.include? 'user'
于 2013-10-22T08:54:03.040 回答
3

我也不知道如何在第 4 章中获得输出。对我来说,使用 sshkit 1.11.1 的 Cap 3.4.0execute并没有这样做。

但是查看 sshkit 文档,这是破解它的一种方法,哪种方法有效:

class StreamOutputInteractionHandler
  def on_data(_command, stream_name, data, channel)
    $stderr.print data
  end
end

# ...

execute :whatever, interaction_handler: StreamOutputInteractionHandler.new

它可能会做一些奇怪的事情,特别是如果你在多个主机上执行,它当然会交错输出。您可以使用类似于内置 MappingInteractionHandler 的方式使用 capistrano 日志,但我想直接打印到控制台,以便在换行符之前获得部分输出(来自 rake 任务的进度条)。

在这里与 sshkit 维护者讨论。https://github.com/capistrano/sshkit/issues/395#issuecomment-288489866

于 2017-03-22T18:11:29.957 回答
-4

在 Capistrano 3.x 中它实际上更简单,你可以这样做:

execute "ls -a"

并且输出将自动流式传输,非常适合流式传输日志文件等。

于 2013-12-19T10:30:54.743 回答