Module: Gitlab::Popen

Extended by:
Popen
Included in:
KeyFingerprint, Popen
Defined in:
lib/gitlab/popen.rb

Instance Method Summary collapse

Instance Method Details

#popen(cmd, path = nil) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/popen.rb', line 8

def popen(cmd, path=nil)
  unless cmd.is_a?(Array)
    raise "System commands must be given as an array of strings"
  end

  path ||= Dir.pwd
  vars = { "PWD" => path }
  options = { chdir: path }

  unless File.directory?(path)
    FileUtils.mkdir_p(path)
  end

  @cmd_output = ""
  @cmd_status = 0
  Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
    # We are not using stdin so we should close it, in case the command we
    # are running waits for input.
    stdin.close
    @cmd_output << stdout.read
    @cmd_output << stderr.read
    @cmd_status = wait_thr.value.exitstatus
  end

  [@cmd_output, @cmd_status]
end