Class: Indocker::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/indocker/shell.rb

Defined Under Namespace

Classes: ShellCommandError, ShellResult

Class Method Summary collapse

Class Method Details

.command(command, logger, skip_errors: false, raise_on_error: false, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/indocker/shell.rb', line 15

def command(command, logger, skip_errors: false, raise_on_error: false, &block)
  if logger.debug?
    logger.debug("Executing command: #{command.cyan}")
  end

  system(command, out: (logger.debug? ? $stdout : File::NULL), err: :out)

  if !$?.success? && !skip_errors
    logger.error("Command: #{command.cyan} failed")

    if raise_on_error
      raise ShellCommandError.new("Command: #{command.cyan} failed")
    else
      exit 1
    end
  end

  if block_given?
    yield
  end
end

.command_exist?(command_name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/indocker/shell.rb', line 55

def command_exist?(command_name)
  `which #{command_name}`
  $?.success?
end

.command_with_result(command, logger, skip_logging: false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/indocker/shell.rb', line 37

def command_with_result(command, logger, skip_logging: false)
  if !skip_logging && logger.debug?
    logger.debug("Executing command: #{command.cyan}")
  end

  result = nil

  IO.popen(command, err: [:child, :out]) do |io|
    result = io.read.chomp.strip

    if !skip_logging
      logger.debug(result)
    end
  end

  ShellResult.new(result, $?.exitstatus)
end