Skip to content

Commit

Permalink
prefixing of each line of output
Browse files Browse the repository at this point in the history
  • Loading branch information
lachie committed Dec 10, 2010
1 parent 3af3410 commit 833a3cd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/eg_helper.rb
Expand Up @@ -3,7 +3,7 @@
require 'exemplor' require 'exemplor'
require 'angry_hash' require 'angry_hash'


root = Pathname(__FILE__).dirname.parent root = Pathname('../..').expand_path(__FILE__)


$LOAD_PATH << root+'examples' $LOAD_PATH << root+'examples'
$LOAD_PATH << root+'lib' $LOAD_PATH << root+'lib'
Expand Down
12 changes: 12 additions & 0 deletions examples/shell.eg.rb
@@ -0,0 +1,12 @@
require 'eg_helper'

eg.helpers do
include CommonMob::ShellHelper
end

eg 'shelly' do
cmd = "echo hello; echo mars >&2; echo world"
Show( sh(cmd ).execute )
Show( sh(cmd, :prefix => "[OXX]" ).execute )
Show( sh(cmd, :prefix => {:err => "[OXX]", :out => "[MXX]"} ).execute )
end
20 changes: 16 additions & 4 deletions lib/common_mob/shell.rb
Expand Up @@ -39,15 +39,27 @@ def initialize(*args,&block)
def execute def execute
error,out = nil,nil error,out = nil,nil


# XXX interleave out and err
rv = popen4(options) {|pid,stdin,stdout,stderr| rv = popen4(options) {|pid,stdin,stdout,stderr|
out = stdout.read out = stdout.read
error = stderr.read error = stderr.read
} }


# TODO print as debug if prefix = options[:prefix]
if Hash === prefix
pre_err = prefix[:err]
pre_out = prefix[:out]
else
pre_err = prefix
pre_out = prefix
end


rv.stderr = error rv.stderr = error.gsub(/^(.*)$/, "#{pre_err}\\1")
rv.stdout = out rv.stdout = out.gsub(/^(.*)$/ , "#{pre_out}\\1")
else
rv.stderr = error
rv.stdout = out
end


rv rv
end end
Expand Down Expand Up @@ -76,7 +88,7 @@ def ok?


def ensure_ok! def ensure_ok!
unless ok? unless ok?
ex = ShellError.new("unable to run options=#{options.inspect}\noutput=#{stdout}\nerror=#{stderr}") ex = ShellError.new("unable to run\noptions=#{options.pretty_inspect}\noutput=#{stdout}\nerror=#{stderr}")
ex.result = self ex.result = self
raise(ex) raise(ex)
end end
Expand Down

0 comments on commit 833a3cd

Please sign in to comment.