lib/cli_tools/system.rb
def sh( cmd, echo = true, capture_output = nil, &block )
  if _sh_captured_output.size == 0 && capture_output.nil? && block.nil?
    # simple form
    puts cmd if echo
    system cmd or raise ShellExecutionError.new("Failed to execute: '#{cmd}' (#{$?})", $?, '')
    return true
  end

  # ok, it's a bit more complex
  exit_status = nil
  current_output = ''
  if echo
    _sh_captured_output.each do |output_channel|
      _sh_capture_concat output_channel, cmd+"\n"
    end
    if capture_output
      _sh_capture_concat capture_output, cmd+"\n"
    else
      puts cmd
    end
  end
  i = STDIN
  Open3.popen2e( cmd ) do |i, oe, t|
    i = STDIN
    oe.sync = true
    while oe_char = oe.getc do
      # puts "sh: block tick"
      _sh_captured_output.each do |output_channel|
        _sh_capture_concat output_channel, oe_char
      end
      if capture_output
        _sh_capture_concat capture_output, oe_char
      else
        putc oe_char
      end
      current_output += oe_char
      if current_output.size > SH_STORED_OUTPUT_LIMIT
        # squeeze cached output
        current_output = current_output.split("\n").last(SH_STORED_OUTPUT_LINES).join("\n")
      end
      yield(oe_char) if block
    end
    exit_status = t.value
  end
  if exit_status != 0
    raise ShellExecutionError.new("Failed to execute: '#{cmd}' (#{exit_status})", exit_status, current_output)
  end
  true
end
# Execs cmd in a shell and returns true if command executed successfully,
# or throws an exception if command failed.
#
Execs cmd in a shell and returns true if command executed successfully,
or throws an exception if command failed.
Execs cmd in a shell and returns true if command executed successfully or throws an exception if command failed .
def sh( cmd, echo = true, capture_output = nil, &block )
  if _sh_captured_output.size == 0 && capture_output.nil? && block.nil?
    puts cmd if echo
    system cmd or raise ShellExecutionError.new("Failed to execute: '#{cmd}' (#{$?})", $?, '')
    return true
  end
  exit_status = nil
  current_output = ''
  if echo
    _sh_captured_output.each do |output_channel|
      _sh_capture_concat output_channel, cmd+"\n"
    end
    if capture_output
      _sh_capture_concat capture_output, cmd+"\n"
    else
      puts cmd
    end
  end
  i = STDIN
  Open3.popen2e( cmd ) do |i, oe, t|
    i = STDIN
    oe.sync = true
    while oe_char = oe.getc do
      _sh_captured_output.each do |output_channel|
        _sh_capture_concat output_channel, oe_char
      end
      if capture_output
        _sh_capture_concat capture_output, oe_char
      else
        putc oe_char
      end
      current_output += oe_char
      if current_output.size > SH_STORED_OUTPUT_LIMIT
        current_output = current_output.split("\n").last(SH_STORED_OUTPUT_LINES).join("\n")
      end
      yield(oe_char) if block
    end
    exit_status = t.value
  end
  if exit_status != 0
    raise ShellExecutionError.new("Failed to execute: '#{cmd}' (#{exit_status})", exit_status, current_output)
  end
  true
end
def sh ( cmd , echo = true , capture_output = nil , & block ) if _sh_captured_output . size == 0 && capture_output . nil? && block . nil? puts cmd if echo system cmd or raise ShellExecutionError . new ( "Failed to execute: '#{cmd}' (#{$?})" , $? , '' ) return true end exit_status = nil current_output = '' if echo _sh_captured_output . each do | output_channel | _sh_capture_concat output_channel , cmd + "\n" end if capture_output _sh_capture_concat capture_output , cmd + "\n" else puts cmd end end i = STDIN Open3 . popen2e ( cmd ) do | i , oe , t | i = STDIN oe . sync = true while oe_char = oe . getc do _sh_captured_output . each do | output_channel | _sh_capture_concat output_channel , oe_char end if capture_output _sh_capture_concat capture_output , oe_char else putc oe_char end current_output += oe_char if current_output . size > SH_STORED_OUTPUT_LIMIT current_output = current_output . split ( "\n" ) . last ( SH_STORED_OUTPUT_LINES ) . join ( "\n" ) end yield ( oe_char ) if block end exit_status = t . value end if exit_status != 0 raise ShellExecutionError . new ( "Failed to execute: '#{cmd}' (#{exit_status})" , exit_status , current_output ) end true end
Execs cmd in a shell and returns true if command executed successfully,
or throws an exception if command failed.
Execs cmd in a shell and returns true if command executed successfully or throws an exception if command failed .
# simple form # ok, it's a bit more complex # puts "sh: block tick" # squeeze cached output
cmd
echo
capture_output
block