Module: Assemblr::Shell

Defined in:
lib/assemblr/shell.rb

Overview

Defines methods for common shell commands.

Class Method Summary collapse

Class Method Details

.exec(cmd, inject: []) ⇒ Array(String, Process::Status)

Execute a command locally.

Parameters:

  • cmd (String)

    the command to run locally

  • inject (Array<String>) (defaults to: [])

    strings to inject into stdin

Returns:

  • (Array(String, Process::Status))


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/assemblr/shell.rb', line 22

def exec(cmd, inject: [])
  log_string = "running `#{cmd}` locally"
  log_string += " with these inputs: #{inject}" unless inject.empty?
  log_info log_string

  result = ''
  status = nil
  Open3.popen2e(cmd) do |i, o, wait|
    inject.each do |line|
      line = line.strip
      i.write line + "\n"
    end
    i.close
    result = o.read
    status = wait.value
  end

  if status.exitstatus != 0
    code = status.exitstatus
    log_error "local command `#{cmd}` exited with a status of #{code}"
  else
    log_success "local command `#{cmd}` executed successfully"
  end

  [result, status]
end

.included(_) ⇒ Object



11
12
13
# File 'lib/assemblr/shell.rb', line 11

def self.included(_)
  expose_method :exec
end