class Cocaine::CommandLine

Attributes

logger[RW]
runner[RW]
exit_status[R]
runner[R]

Public Class Methods

environment() click to toggle source
# File lib/cocaine/command_line.rb, line 17
def environment
  @supplemental_environment ||= {}
end
fake!() click to toggle source
# File lib/cocaine/command_line.rb, line 29
def fake!
  @runner = FakeRunner.new
end
new(binary, params = "", options = {}) click to toggle source
# File lib/cocaine/command_line.rb, line 50
def initialize(binary, params = "", options = {})
  @binary            = binary.dup
  @params            = params.dup
  @options           = options.dup
  @runner            = @options.delete(:runner) || self.class.runner
  @logger            = @options.delete(:logger) || self.class.logger
  @swallow_stderr    = @options.delete(:swallow_stderr)
  @expected_outcodes = @options.delete(:expected_outcodes) || [0]
  @environment       = @options.delete(:environment) || {}
  @runner_options    = @options.delete(:runner_options) || {}
end
path() click to toggle source
# File lib/cocaine/command_line.rb, line 7
def path
  @supplemental_path
end
path=(supplemental_path) click to toggle source
# File lib/cocaine/command_line.rb, line 11
def path=(supplemental_path)
  @supplemental_path = Array(supplemental_path).
    flatten.
    join(OS.path_separator)
end
runner_options() click to toggle source
# File lib/cocaine/command_line.rb, line 25
def runner_options
  @default_runner_options ||= {}
end
unfake!() click to toggle source
# File lib/cocaine/command_line.rb, line 33
def unfake!
  @runner = nil
end

Private Class Methods

best_runner() click to toggle source
# File lib/cocaine/command_line.rb, line 39
def best_runner
  [PosixRunner, ProcessRunner, BackticksRunner].detect do |runner|
    runner.supported?
  end.new
end

Public Instance Methods

command(interpolations = {}) click to toggle source
# File lib/cocaine/command_line.rb, line 62
def command(interpolations = {})
  cmd = [path_prefix, @binary, interpolate(@params, interpolations)]
  cmd << bit_bucket if @swallow_stderr
  cmd.join(" ").strip
end
run(interpolations = {}) click to toggle source
# File lib/cocaine/command_line.rb, line 68
def run(interpolations = {})
  output = ''
  @exit_status = nil
  begin
    full_command = command(interpolations)
    log("#{colored("Command")} :: #{full_command}")
    output = execute(full_command)
  rescue Errno::ENOENT => e
    raise Cocaine::CommandNotFoundError, e.message
  ensure
    @exit_status = $?.respond_to?(:exitstatus) ? $?.exitstatus : 0
  end

  if @exit_status == 127
    raise Cocaine::CommandNotFoundError
  end

  unless @expected_outcodes.include?(@exit_status)
    message = [
      "Command '#{full_command}' returned #{@exit_status}. Expected #{@expected_outcodes.join(", ")}",
      "Here is the command output:\n",
      output
    ].join("\n")
    raise Cocaine::ExitStatusError, message
  end
  output
end

Private Instance Methods

bit_bucket() click to toggle source
# File lib/cocaine/command_line.rb, line 179
def bit_bucket
  OS.unix? ? "2>/dev/null" : "2>NUL"
end
colored(text, ansi_color = "\e[32m") click to toggle source
# File lib/cocaine/command_line.rb, line 98
def colored(text, ansi_color = "\e[32m")
  if @logger && @logger.respond_to?(:tty?) && @logger.tty?
    "#{ansi_color}#{text}\e[0m"
  else
    text
  end
end
environment() click to toggle source
# File lib/cocaine/command_line.rb, line 138
def environment
  self.class.environment.merge(@environment)
end
execute(command) click to toggle source
# File lib/cocaine/command_line.rb, line 134
def execute(command)
  runner.call(command, environment, runner_options)
end
interpolate(pattern, interpolations) click to toggle source
# File lib/cocaine/command_line.rb, line 146
def interpolate(pattern, interpolations)
  interpolations = stringify_keys(interpolations)
  pattern.gsub(/:\{?(\w+)\b\}?/) do |match|
    key = match.tr(":{}", "")
    if interpolations.key?(key)
      shell_quote_all_values(interpolations[key])
    else
      match
    end
  end
end
log(text) click to toggle source
# File lib/cocaine/command_line.rb, line 106
def log(text)
  if @logger
    @logger.info(text)
  end
end
os_path_prefix() click to toggle source
# File lib/cocaine/command_line.rb, line 118
def os_path_prefix
  if OS.unix?
    unix_path_prefix
  else
    windows_path_prefix
  end
end
path_prefix() click to toggle source
# File lib/cocaine/command_line.rb, line 112
def path_prefix
  if !self.class.path.nil? && !self.class.path.empty?
    os_path_prefix
  end
end
runner_options() click to toggle source
# File lib/cocaine/command_line.rb, line 142
def runner_options
  self.class.runner_options.merge(@runner_options)
end
shell_quote(string) click to toggle source
# File lib/cocaine/command_line.rb, line 166
def shell_quote(string)
  return "" if string.nil?
  if OS.unix?
    if string.empty?
      "''"
    else
      string.split("'").map{|m| "'#{m}'" }.join("\\'")
    end
  else
    %Q{"#{string}"}
  end
end
shell_quote_all_values(values) click to toggle source
# File lib/cocaine/command_line.rb, line 162
def shell_quote_all_values(values)
  Array(values).map(&method(:shell_quote)).join(" ")
end
stringify_keys(hash) click to toggle source
# File lib/cocaine/command_line.rb, line 158
def stringify_keys(hash)
  Hash[hash.map{ |k, v| [k.to_s, v] }]
end
unix_path_prefix() click to toggle source
# File lib/cocaine/command_line.rb, line 126
def unix_path_prefix
  "PATH=#{self.class.path}#{OS.path_separator}$PATH;"
end
windows_path_prefix() click to toggle source
# File lib/cocaine/command_line.rb, line 130
def windows_path_prefix
  "SET PATH=#{self.class.path}#{OS.path_separator}%PATH% &"
end