class Indocker::Shell

Public Class Methods

command(command, logger, skip_errors: false, raise_on_error: false) { || ... } click to toggle source
# File lib/indocker/shell.rb, line 15
def command(command, logger, skip_errors: false, raise_on_error: false, &block)
  if logger.debug?
    logger.debug("Executing command: #{command.cyan}")
  end

  system(command, out: (logger.debug? ? $stdout : File::NULL), err: :out)

  if !$?.success? && !skip_errors
    logger.error("Command: #{command.cyan} failed")

    if raise_on_error
      raise ShellCommandError.new("Command: #{command.cyan} failed")
    else
      exit 1
    end
  end

  if block_given?
    yield
  end
end
command_exist?(command_name) click to toggle source
# File lib/indocker/shell.rb, line 55
def command_exist?(command_name)
  `which #{command_name}`
  $?.success?
end
command_with_result(command, logger, skip_logging: false) click to toggle source
# File lib/indocker/shell.rb, line 37
def command_with_result(command, logger, skip_logging: false)
  if !skip_logging && logger.debug?
    logger.debug("Executing command: #{command.cyan}")
  end

  result = nil

  IO.popen(command, err: [:child, :out]) do |io|
    result = io.read.chomp.strip

    if !skip_logging
      logger.debug(result)
    end
  end

  ShellResult.new(result, $?.exitstatus)
end