Class: Hanami::CLI::SystemCall

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/cli/system_call.rb

Overview

Facility for making convenient system calls and returning their results.

Since:

  • 2.0.0

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#call(cmd, env: {}) ⇒ Result #call(cmd, env: {}, &blk) ⇒ Result

Executes the given system command and returns the result.

Overloads:

  • #call(cmd, env: {}, &blk) ⇒ Result

    Executes the command and passes the given block to the Open3.popen3 method called internally.

    Examples:

    call("info") do |stdin, stdout, stderr, wait_thread|
      # ...
    end

Parameters:

  • cmd (String)

    the system command to execute

  • env (Hash<String, String>) (defaults to: {})

    an optional hash of environment variables to set before executing the command

Returns:

Since:

  • 2.0.0



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

Since:

  • 2.1.0



110
111
112
# File 'lib/hanami/cli/system_call.rb', line 110

def command(cmd, *args)
  [cmd, args].flatten(1).compact.join(" ")
end