24

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

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).

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.

Richard Johansson
  • 430
  • 1
  • 5
  • 12

2 Answers2

40

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

Heikki
  • 15,329
  • 2
  • 54
  • 49
  • 2
    Update from future: `capture` seems to work with Capistrano 3 too, even through I wasn’t able to find a mention about it in the documentation... – Smar Feb 28 '17 at 14:27
6

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
gmsharky
  • 131
  • 2
  • 5