Module: Bsdkrun::Process

Defined in:
lib/bsdkrun/process.rb

Overview

Spawns the bsdkrun CLI and captures its output.

Every invocation is prefixed with --log-level (default 0) so the SDK's captured output stays clean.

Defined Under Namespace

Classes: RawResult

Class Method Summary collapse

Class Method Details

.run(args, env: {}, stdin: nil, log_level: 0, on_stdout: nil, on_stderr: nil) ⇒ RawResult

Run bsdkrun --log-level <n> <args> to completion, buffering output.

Parameters:

  • args (Array<String>)

    CLI arguments (without the binary).

  • env (Hash) (defaults to: {})

    extra environment merged onto the process env.

  • stdin (String, nil) (defaults to: nil)

    data piped to the child's stdin.

  • log_level (Integer) (defaults to: 0)

    bsdkrun global log level (0=off .. 5=trace).

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bsdkrun/process.rb', line 30

def run(args, env: {}, stdin: nil, log_level: 0, on_stdout: nil, on_stderr: nil)
  bin = Binary.resolve
  full = ["--log-level", log_level.to_s, *args]
  merged_env = env.to_h.transform_keys(&:to_s).transform_values(&:to_s)
  out = +""
  err = +""
  status = nil
  Open3.popen3(merged_env, bin, *full) do |child_in, child_out, child_err, wait|
    writer = Thread.new { child_in.write(stdin) if stdin; child_in.close }
    stdout_reader = Thread.new do
      while (chunk = child_out.readpartial(8192) rescue nil)
        out << chunk
        on_stdout&.call(chunk)
      end
    end
    stderr_reader = Thread.new do
      while (chunk = child_err.readpartial(8192) rescue nil)
        err << chunk
        on_stderr&.call(chunk)
      end
    end
    [writer, stdout_reader, stderr_reader].each(&:join)
    status = wait.value
  end
  RawResult.new(stdout: out, stderr: err, exit_code: status.exitstatus || 0)
end

.run!(args, label:, **opts) ⇒ RawResult

Run and raise CommandFailed on a non-zero exit.

Parameters:

  • args (Array<String>)
  • label (String)

    human label used in the error.

Returns:

Raises:



63
64
65
66
67
68
69
70
71
# File 'lib/bsdkrun/process.rb', line 63

def run!(args, label:, **opts)
  res = run(args, **opts)
  unless res.exit_code.zero?
    raise CommandFailed.new(
      exit_code: res.exit_code, stdout: res.stdout, stderr: res.stderr, command: label
    )
  end
  res
end

.spawn_interactive(args, log_level: 0) ⇒ Boolean

Spawn an interactive bsdkrun command inheriting the parent's stdio and wait for it (for shell). Returns the child's exit status boolean.

Parameters:

  • args (Array<String>)
  • log_level (Integer) (defaults to: 0)

Returns:

  • (Boolean)

    true if the command exited zero.



79
80
81
82
83
# File 'lib/bsdkrun/process.rb', line 79

def spawn_interactive(args, log_level: 0)
  bin = Binary.resolve
  full = ["--log-level", log_level.to_s, *args]
  system(bin, *full)
end