Module: Executioner

Defined in:
lib/executioner.rb

Defined Under Namespace

Modules: ClassMethods Classes: ExecutableNotFoundError, ExecutionerError, ProcessError

Constant Summary collapse

SEARCH_PATHS =
%w(/bin /usr/bin /usr/local/bin /opt/homebrew/bin /opt/local/bin)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.loggerObject

Assign a logger if you want to log commands and options.



16
17
18
# File 'lib/executioner.rb', line 16

def logger
  @logger
end

Class Method Details

.execute(command, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/executioner.rb', line 23

def execute(command, options={})
  command = "#{options[:env].map { |k,v| "#{k}='#{v}'" }.join(' ')} #{command}" if options[:env]
  
  Executioner.logger.debug("Executing: `#{command}'") if Executioner.logger
  
  output = nil
  Open3.popen3(command) do |stdin, stdout, stderr|
    stdout, stderr = stderr, stdout if options[:switch_stdout_and_stderr]
    
    output = stdout.gets(nil)
    if output.nil? && (error_message = stderr.gets(nil))
      if error_message =~ /:in\s`exec':\s(.+)\s\(.+\)$/
        error_message = $1
      end
      raise ProcessError, "Command: \"#{command}\"\nOutput: \"#{error_message.chomp}\""
    end
  end
  output
end

.included(klass) ⇒ Object



18
19
20
# File 'lib/executioner.rb', line 18

def included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#concat_args(args) ⇒ Object



44
45
46
# File 'lib/executioner.rb', line 44

def concat_args(args)
  args.map { |a,v| "-#{a} #{v}" }.join(' ')
end

#execute_queued(options = {}) ⇒ Object



57
58
59
60
# File 'lib/executioner.rb', line 57

def execute_queued(options={})
  execute(queued_commands, options)
  @commands = []
end

#queue(command) ⇒ Object



48
49
50
51
# File 'lib/executioner.rb', line 48

def queue(command)
  @commands ||= []
  @commands << command
end

#queued_commandsObject



53
54
55
# File 'lib/executioner.rb', line 53

def queued_commands
  @commands ? @commands.join(' && ') : ''
end