Sha256: eefcd60bb5df36d78b26571f5b061ced273d7bf8f11a0b07cd62c2ba8361c1fa

Contents?: true

Size: 1.02 KB

Versions: 53

Compression:

Stored size: 1.02 KB

Contents

require 'shellwords'

module Dragonfly
  module Shell
    
    include Configurable
    configurable_attr :log_commands, false

    # Exceptions
    class CommandFailed < RuntimeError; end

    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
  
    def raise_shell_command_failed(command)
      raise CommandFailed, "Command failed (#{command}) with exit status #{$?.exitstatus}"
    end
    
    def escape_args(args)
      args.shellsplit.map do |arg|
        quote arg.gsub(/\\?'/, %q('\\\\''))
      end.join(' ')
    end
    
    def quote(string)
      q = Dragonfly.running_on_windows? ? '"' : "'"
      q + string + q
    end

  end
end

Version data entries

53 entries across 53 versions & 2 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.7.4 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.7.3 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.7.2 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.7.1 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.7.0 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
dragonfly-0.9.15 lib/dragonfly/shell.rb
dragonfly-0.9.14 lib/dragonfly/shell.rb
dragonfly-0.9.13 lib/dragonfly/shell.rb
classiccms-0.6.9 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.6.8 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.6.7 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.6.6 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.6.5 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.6.4 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.6.3 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.6.2 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.6.1 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.6.0 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb
classiccms-0.5.17 vendor/bundle/gems/dragonfly-0.9.12/lib/dragonfly/shell.rb