如何在Capistrano中捕获远程命令的输出?

时间:2022-04-11 07:25:16

I want to run a remote command (git diff of current_revision and HEAD in a few folders) and capture the output.

我想运行一个远程命令(在几个文件夹中的current_revision和HEAD的git diff)并捕获输出。

I've tried run("git diff rev1 rev2 -- folder | cat"), but the method always returns seems to return nil (even when I can see the diff output in the Capistrano output).

我试过run(“git diff rev1 rev2 - folder | cat”),但方法总是返回似乎返回nil(即使我可以在Capistrano输出中看到diff输出)。

Any ideas? Can I use different means of piping the command, or anything like that? Im not a Unix wizard, so it could be something trivial Im missing here.

有任何想法吗?我可以使用不同的方法来管理命令,或类似的东西吗?我不是一个Unix巫师,所以它可能是我在这里失踪的微不足道的东西。

2 个解决方案

#1


39  

Maybe capture?

也许捕获?

"The capture helper will execute the given command on the first matching server, and will return the output of the command as a string."

“捕获助手将在第一个匹配的服务器上执行给定的命令,并将命令的输出作为字符串返回。”

https://github.com/capistrano/capistrano/wiki/2.x-DSL-Action-Inspection-Capture

https://github.com/capistrano/capistrano/wiki/2.x-DSL-Action-Inspection-Capture

#2


5  

If you want to capture the output of multiple hosts, use run with a block: e.g.:

如果要捕获多个主机的输出,请使用带有块的运行:例如:

 desc "capture output from multiple servers"
  task :capture_multiple_servers, :roles => [:some_servers] do
    results = {}
    run "hostname --fqdn" do |channel, stream, data|
      if stream == :out
        results[channel[:host]] = [] unless results.key?(channel[:host])
        results[channel[:host]] << data if stream == :out
      end
    end
    puts "Your results were:"
    results.keys.sort.each do | host |
      puts "#{host}:#{results[host].join}"
    end
  end

#1


39  

Maybe capture?

也许捕获?

"The capture helper will execute the given command on the first matching server, and will return the output of the command as a string."

“捕获助手将在第一个匹配的服务器上执行给定的命令,并将命令的输出作为字符串返回。”

https://github.com/capistrano/capistrano/wiki/2.x-DSL-Action-Inspection-Capture

https://github.com/capistrano/capistrano/wiki/2.x-DSL-Action-Inspection-Capture

#2


5  

If you want to capture the output of multiple hosts, use run with a block: e.g.:

如果要捕获多个主机的输出,请使用带有块的运行:例如:

 desc "capture output from multiple servers"
  task :capture_multiple_servers, :roles => [:some_servers] do
    results = {}
    run "hostname --fqdn" do |channel, stream, data|
      if stream == :out
        results[channel[:host]] = [] unless results.key?(channel[:host])
        results[channel[:host]] << data if stream == :out
      end
    end
    puts "Your results were:"
    results.keys.sort.each do | host |
      puts "#{host}:#{results[host].join}"
    end
  end