Sha256: 64c38f4f1ab292ab742804e715cc843fe77113ccc2f7c720f3585ed91798320b

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'shellwords'
require 'open4'

module HalfShell
  
  class ExecuteError < StandardError
    
    attr_reader :command, :status
    
    def initialize(command, status)
      @command, @status = command, status
      super "command <#{@command}> failed with status <#{@status}>"
    end
    
  end

  class << self
    
    def execute(*args)
      opts = args.last.is_a?(Hash) ? args.pop : {}
      opts[:raise] = true unless opts.has_key? :raise
      
      command = args.join(' ')
      
      stdout, stderr = nil
      status = Open4.popen4(command) do |pid, _stdin, _stdout, _stderr|
        stdout = _stdout.read
        stderr = _stderr.read
      end
      
      if opts[:raise] && status.exitstatus != 0
        raise ExecuteError.new command, status.exitstatus
      end
      
      Result.new command, status.exitstatus, stdout, stderr
    end
    
    def execute!(*args)
      command = args.join(' ')
      `#{command}`
      $?.success?
    end
    
  end

end

require File.expand_path '../half_shell/result', __FILE__

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
half_shell-0.0.3 lib/half_shell.rb