class Dockerhelper::Command

Attributes

chdir[R]
cmd[R]
label[R]

Public Class Methods

new(cmd, label: '', chdir: Dir.pwd) click to toggle source
# File lib/dockerhelper/command.rb, line 7
def initialize(cmd, label: '', chdir: Dir.pwd)
  @label = " #{label}" if label.size > 0
  @cmd = cmd
  @chdir = chdir
end

Public Instance Methods

capture() click to toggle source
# File lib/dockerhelper/command.rb, line 13
def capture
  stdout, stderr, status = Open3.capture3(cmd)
  pid = status.pid
  cmd_prefix = "[#{pid}#{label}]"

  $stdout.puts ">> #{yellow(cmd_prefix)} cwd: #{chdir} cmd: #{cmd}"
  unless stdout.empty?
    stdout.lines.each do |line|
      $stdout.puts "   #{green(cmd_prefix)} #{line}"
    end
  end

  unless stderr.empty?
    stderr.lines.each do |line|
      $stderr.puts "   #{red(cmd_prefix)} #{line}"
    end
  end

  $stdout.puts "<< #{yellow(cmd_prefix)} exit_status: #{status}"
  stdout
end
colorize(text, color_code) click to toggle source
# File lib/dockerhelper/command.rb, line 62
def colorize(text, color_code)
  "\e[#{color_code}m#{text}\e[0m"
end
green(text) click to toggle source
# File lib/dockerhelper/command.rb, line 67
def green(text); colorize(text, 32); end
red(text) click to toggle source
# File lib/dockerhelper/command.rb, line 66
def red(text); colorize(text, 31); end
run() click to toggle source
# File lib/dockerhelper/command.rb, line 35
def run
  Open3.popen3(cmd, chdir: chdir) do |stdin, stdout, stderr, wait_thr|
    pid = wait_thr.pid
    cmd_prefix = "[#{pid}#{label}]"
    $stdout.puts ">> #{yellow(cmd_prefix)} cwd: #{chdir} cmd: #{cmd}"

    stdin.close
    stderr_thr = Thread.new do
      while line = stderr.gets
        $stderr.puts "   #{red(cmd_prefix)} #{line}"
      end
    end

    stdout_thr = Thread.new do
      while line = stdout.gets
        $stdout.puts "   #{green(cmd_prefix)} #{line}"
      end
    end

    stderr_thr.join
    stdout_thr.join

    exit_status = wait_thr.value
    $stdout.puts "<< #{yellow(cmd_prefix)} exit_status: #{exit_status}"
  end
end
yellow(text) click to toggle source
# File lib/dockerhelper/command.rb, line 68
def yellow(text); colorize(text, 33); end