Class: Hanami::CLI::SystemCall
- Inherits:
-
Object
- Object
- Hanami::CLI::SystemCall
- Defined in:
- lib/hanami/cli/system_call.rb
Overview
Facility for making convenient system calls and returning their results.
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
-
#call(cmd, *args, env: {}) ⇒ Result
Executes the given system command and returns the result.
- #command(cmd, *args) ⇒ Object
Instance Method Details
#call(cmd, env: {}) ⇒ Result #call(cmd, env: {}, &blk) ⇒ Result
Executes the given system command and returns the result.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/hanami/cli/system_call.rb', line 88 def call(cmd, *args, env: {}) exitstatus = nil out = nil err = nil ::Bundler.with_unbundled_env do Open3.popen3(env, command(cmd, *args)) do |stdin, stdout, stderr, wait_thr| yield stdin, stdout, stderr, wait_thr if block_given? stdin.close exitstatus = wait_thr&.value&.exitstatus out = Thread.new { stdout.read }.value.strip err = Thread.new { stderr.read }.value.strip end end Result.new(exit_code: exitstatus, out: out, err: err) end |
#command(cmd, *args) ⇒ Object
110 111 112 |
# File 'lib/hanami/cli/system_call.rb', line 110 def command(cmd, *args) [cmd, args].flatten(1).compact.join(" ") end |