thoughtbot / cocaine

A small library for doing (command) lines.
https://robots.thoughtbot.com
Other
785 stars 55 forks source link

How to obtain command output with Cocaine? #66

Closed mrjamesriley closed 10 years ago

mrjamesriley commented 10 years ago

Hello there,

Not sure if I'm missing something crazy obvious here.

If I run a simple command in terminal like: ls blah, then I get the following output: ls: blah: No such file or directory, given that the 'blah' does not exist in my case, of course.

Yet, when running Cocaine::CommandLine.new('ls', 'blah').run - I see no way to access the error message above. The ExitStatusError exception gives me a message like:

Cocaine::ExitStatusError: Command 'ls blah' returned 1. Expected 0
Here is the command output:

    from /Users/james/.rvm/gems/ruby-1.9.2-p320/gems/cocaine-0.5.4/lib/cocaine/command_line.rb:96:in `run'
    from (irb):3
    from /Users/james/.rvm/rubies/ruby-1.9.2-p320/bin/irb:16:in `<main>'

The output of the run command gives me nil. And when using the :logger option, all I get is:

# Logfile created on 2014-05-21 06:27:32 +0100 by logger.rb/25413
I, [2014-05-21T06:27:32.336104 #81036]  INFO -- : Command :: ls blah

This is for a script I'm written, where I want to log the actual output of the failed command, otherwise I've got little to work with when reviewing logs later.

So, what to do?

Thanks for a great Gem by the way, it's otherwise a joy to use.

cheers, James

jyurek commented 10 years ago

run doesn't capture output other than stdout. You're not seeing it in the log because it's going to stderr. If you have a good way of capturing and returning stderr and stdout separately, I'd love to hear it. Unfortunately, I'm not entirely sure there is one. Maybe popen4, but I'm not sure that's maintained anymore. Sorry about that. I'd be willing to entertain PRs if you come up with something, though.

jyurek commented 10 years ago

You know, I just realized this could be done fairly easily for the process-spawn and posix-spawn runners. It will require ignoring the result of run and keeping the CommandLine object around for inspection, but it should be pretty straightforward. I'll work on that this morning.

jyurek commented 10 years ago

I have a PR in #67 so if you'd like to comment on that with additions, comments, complaints, that would be great. I'm going to close this in favor of continuing discussion there. Thanks.