Module: Dragonfly::Shell

Defined in:
lib/dragonfly/shell.rb

Defined Under Namespace

Classes: CommandFailed

Instance Method Summary collapse

Instance Method Details

#escape_args(args) ⇒ Object



32
33
34
35
36
# File 'lib/dragonfly/shell.rb', line 32

def escape_args(args)
  args.shellsplit.map do |arg|
    quote arg.gsub(/\\?'/, %q('\\\\''))
  end.join(' ')
end

#quote(string) ⇒ Object



38
39
40
41
# File 'lib/dragonfly/shell.rb', line 38

def quote(string)
  q = Dragonfly.running_on_windows? ? '"' : "'"
  q + string + q
end

#raise_shell_command_failed(command) ⇒ Object

Raises:



28
29
30
# File 'lib/dragonfly/shell.rb', line 28

def raise_shell_command_failed(command)
  raise CommandFailed, "Command failed (#{command}) with exit status #{$?.exitstatus}"
end

#run(command, args = "") ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dragonfly/shell.rb', line 12

def run(command, args="")
  full_command = "#{command} #{escape_args(args)}"
  log.debug("Running command: #{full_command}") if log_commands
  begin
    result = `#{full_command}`
  rescue Errno::ENOENT
    raise_shell_command_failed(full_command)
  end
  if $?.exitstatus == 1
    throw :unable_to_handle
  elsif !$?.success?
    raise_shell_command_failed(full_command)
  end
  result
end