Module: Vow

Defined in:
lib/vow.rb

Overview

Overview

Vow is a Kernel.system() wrapper. Vow makes it more convenient to construct external commands to run and deal with their exit codes.

Usage

In the simplest form, Vow.system() can be used as a drop-in replacement for Kernel.system():

# Execute command
Vow.system('echo', 'Hello, World!')

Defined Under Namespace

Modules: Helper

Class Method Summary collapse

Class Method Details

.silent(*command, **args) ⇒ Object



44
45
46
47
48
# File 'lib/vow.rb', line 44

def self.silent(*command, **args)
  args[:out] = '/dev/null'
  args[:err] = '/dev/null'
  self.system(*command, **args)
end

.strict(*command, **args) ⇒ Object



50
51
52
53
# File 'lib/vow.rb', line 50

def self.strict(*command, **args)
  args[:may_raise] = true
  self.system(*command, **args)
end

.system(*command, **args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vow.rb', line 16

def self.system(*command, **args)
  may_raise = args.delete(:may_raise)

  expected_exit_codes = (args.delete(:returns) or 0)
  expected_exit_codes = [expected_exit_codes].flatten

  command = Helper.refine_command(command)

  if Kernel.system(*command, **args).nil?
    if may_raise
	raise "Failed to execute command: #{Helper.printable_command(command)}"
    else
	return nil
    end
  end

  exit_code = $?.exitstatus
  if expected_exit_codes.include?(exit_code)
    return true
  else
    if may_raise
	raise "Unexpected error code (#{exit_code}) from command: #{Helper.printable_command(command)}"
    else
	return false
    end
  end
end