2013-10-08 18:36:16 +00:00
|
|
|
require 'fileutils'
|
2014-02-25 10:57:42 +00:00
|
|
|
require 'open3'
|
2013-10-08 18:36:16 +00:00
|
|
|
|
2013-01-28 15:22:45 +00:00
|
|
|
module Gitlab
|
|
|
|
module Popen
|
2014-02-25 10:58:58 +00:00
|
|
|
extend self
|
|
|
|
|
2016-08-06 02:03:01 +00:00
|
|
|
def popen(cmd, path = nil)
|
2014-02-25 10:57:42 +00:00
|
|
|
unless cmd.is_a?(Array)
|
|
|
|
raise "System commands must be given as an array of strings"
|
|
|
|
end
|
|
|
|
|
2014-02-25 10:58:22 +00:00
|
|
|
path ||= Dir.pwd
|
2013-01-28 15:22:45 +00:00
|
|
|
vars = { "PWD" => path }
|
2013-05-05 14:01:10 +00:00
|
|
|
options = { chdir: path }
|
2013-01-28 15:22:45 +00:00
|
|
|
|
2013-10-08 18:36:16 +00:00
|
|
|
unless File.directory?(path)
|
2013-10-08 18:53:22 +00:00
|
|
|
FileUtils.mkdir_p(path)
|
2013-10-08 18:36:16 +00:00
|
|
|
end
|
|
|
|
|
2013-01-28 15:22:45 +00:00
|
|
|
@cmd_output = ""
|
|
|
|
@cmd_status = 0
|
2014-02-25 10:57:42 +00:00
|
|
|
Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
|
2016-08-30 14:06:40 +00:00
|
|
|
yield(stdin) if block_given?
|
2014-12-17 11:18:11 +00:00
|
|
|
stdin.close
|
2016-08-30 14:06:40 +00:00
|
|
|
|
2013-01-28 15:22:45 +00:00
|
|
|
@cmd_output << stdout.read
|
|
|
|
@cmd_output << stderr.read
|
2014-02-25 10:57:42 +00:00
|
|
|
@cmd_status = wait_thr.value.exitstatus
|
2013-01-28 15:22:45 +00:00
|
|
|
end
|
|
|
|
|
2015-03-25 01:35:57 +00:00
|
|
|
[@cmd_output, @cmd_status]
|
2013-01-28 15:22:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|