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
-
.run(args, env: {}, stdin: nil, log_level: 0, on_stdout: nil, on_stderr: nil, binary: false) ⇒ RawResult
Run
bsdkrun --log-level <n> <args>to completion, buffering output. -
.run!(args, label:, **opts) ⇒ RawResult
Run and raise CommandFailed on a non-zero exit.
-
.spawn_interactive(args, log_level: 0) ⇒ Boolean
Spawn an interactive
bsdkruncommand inheriting the parent's stdio and wait for it (forshell).
Class Method Details
.run(args, env: {}, stdin: nil, log_level: 0, on_stdout: nil, on_stderr: nil, binary: false) ⇒ RawResult
Run bsdkrun --log-level <n> <args> to completion, buffering output.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/bsdkrun/process.rb', line 34 def run(args, env: {}, stdin: nil, log_level: 0, on_stdout: nil, on_stderr: nil, binary: false) 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 = binary ? (+"").b : +"" err = +"" status = nil Open3.popen3(merged_env, bin, *full) do |child_in, child_out, child_err, wait| if binary child_in.binmode child_out.binmode end 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.
71 72 73 74 75 76 77 78 79 |
# File 'lib/bsdkrun/process.rb', line 71 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.
87 88 89 90 91 |
# File 'lib/bsdkrun/process.rb', line 87 def spawn_interactive(args, log_level: 0) bin = Binary.resolve full = ["--log-level", log_level.to_s, *args] system(bin, *full) end |