class Ellipses::Support::Shell::Runner

Adapted to popen3 from github.com/mina-deploy/mina

Public Class Methods

new() click to toggle source
# File lib/ellipses/support/shell.rb, line 28
def initialize
  @coathooks = 0
end

Public Instance Methods

run(*args) click to toggle source
# File lib/ellipses/support/shell.rb, line 32
def run(*args)
  return dummy_result if args.empty?

  out, err, status =
    Open3.popen3(*args) do |_, stdout, stderr, wait_thread|
      block(stdout, stderr, wait_thread)
    end
  Result.new args, out, err, status.exitstatus
end

Private Instance Methods

block(stdout, stderr, wait_thread) click to toggle source
# File lib/ellipses/support/shell.rb, line 44
def block(stdout, stderr, wait_thread)
  # Handle `^C`
  trap('INT') { handle_sigint(wait_thread.pid) }

  out = stdout.readlines.map(&:chomp)
  err = stderr.readlines.map(&:chomp)

  [out, err, wait_thread.value]
end
handle_sigint(pid) click to toggle source
# File lib/ellipses/support/shell.rb, line 54
def handle_sigint(pid) # rubocop:disable Metrics/MethodLength
  message, signal = if @coathooks > 1
                      ['SIGINT received again. Force quitting...', 'KILL']
                    else
                      ['SIGINT received.', 'TERM']
                    end

  warn
  warn message
  ::Process.kill signal, pid
  @coathooks += 1
rescue Errno::ESRCH
  warn 'No process to kill.'
end