Module: Rabbit::SystemRunner

Class Method Summary collapse

Class Method Details

.run(cmd, *args, progress: nil) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/rabbit/utils.rb', line 355

def run(cmd, *args, progress: nil)
  begin
    IO.pipe do |input, output|
      pid = spawn(cmd, *args, out: output)
      output.close
      begin
        loop do
          readables, = IO.select([input], nil, nil, 0.1)
          if readables
            readable = readables[0]
            begin
              $stdout.print(readable.read_nonblock(4096))
            rescue EOFError
              break
            else
              break if readable.eof?
            end
          else
            progress.call if progress
          end
        end
        true
      ensure
        Process.waitpid(pid)
      end
    end
  rescue SystemCallError
    yield($!) if block_given?
    false
  end
end