bsdkrun 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b23de4b48a88b521c5909ad2aa610d8557e9b06661e450db3d862292cc3bb296
4
- data.tar.gz: be6175dd5884c7ec861a57f355c3dff05d01e759355117d8e21e27904b717063
3
+ metadata.gz: 14072fa1b965040c136001ee65a8ef384205cbc119ef91658c387e2ed84100fb
4
+ data.tar.gz: c3981e13c6a964c084b3a1b54a051801ab373eda107f74dd869e789242c744df
5
5
  SHA512:
6
- metadata.gz: 9b2b575fc9be711c375a053537e8ba687d162c4180c1e7075aaa5e3d27d8992655615c10c28ca5894a066213a3d8382c9f6f7c31fdca7118b8dc72707bddf86f
7
- data.tar.gz: 38d9ba56709b1b870df9873d70c658e68534fb2eb90fa6c6793a2e87aa80d9fe1f076a6324579f4834a9241554960b35c6501ebc05e3896a781da3166be2c565
6
+ metadata.gz: 5dcbfd7f46c6e412acc0eccc6ccbe639fdffaf875a8ac56e8af0bbac4ec4654804a149f8eff78316ad0ca06cf2338414de320cf6ba05ca8a8ea11d579167c92e
7
+ data.tar.gz: 165af797cc3af4b111c9fad1c02f8f1b788d1e900e1d49715e8547b02e8849b768b21ed3cc81d4f922accb073b46f33f7400d7ff854f5868de92869802d3699a
data/README.md CHANGED
@@ -91,6 +91,8 @@ box.exec("ruby",
91
91
  cwd: "/app",
92
92
  stdin: "data on stdin",
93
93
  tty: true, # allocate a PTY
94
+ on_stdout: ->(chunk) { $stdout.write(chunk) },
95
+ on_stderr: ->(chunk) { $stderr.write(chunk) },
94
96
  throw_on_error: true) # raise on non-zero exit (default: false)
95
97
 
96
98
  # Vercel-Sandbox-style alias:
@@ -105,6 +107,10 @@ result.lines # non-empty stdout lines
105
107
  `exec` returns a `Bsdkrun::Result`. It only raises `CommandFailed` when you pass
106
108
  `throw_on_error: true` (or call `result.throw_if_failed!`).
107
109
 
110
+ The callbacks run as chunks arrive, and the same bytes remain buffered in the
111
+ returned result. They do not require `tty`; a PTY changes command semantics and
112
+ may merge stderr into stdout.
113
+
108
114
  ## Lifecycle & inventory
109
115
 
110
116
  ```ruby
@@ -27,11 +27,30 @@ module Bsdkrun
27
27
  # @param stdin [String, nil] data piped to the child's stdin.
28
28
  # @param log_level [Integer] bsdkrun global log level (0=off .. 5=trace).
29
29
  # @return [RawResult]
30
- def run(args, env: {}, stdin: nil, log_level: 0)
30
+ def run(args, env: {}, stdin: nil, log_level: 0, on_stdout: nil, on_stderr: nil)
31
31
  bin = Binary.resolve
32
32
  full = ["--log-level", log_level.to_s, *args]
33
33
  merged_env = env.to_h.transform_keys(&:to_s).transform_values(&:to_s)
34
- out, err, status = Open3.capture3(merged_env, bin, *full, stdin_data: stdin || "")
34
+ out = +""
35
+ err = +""
36
+ status = nil
37
+ Open3.popen3(merged_env, bin, *full) do |child_in, child_out, child_err, wait|
38
+ writer = Thread.new { child_in.write(stdin) if stdin; child_in.close }
39
+ stdout_reader = Thread.new do
40
+ while (chunk = child_out.readpartial(8192) rescue nil)
41
+ out << chunk
42
+ on_stdout&.call(chunk)
43
+ end
44
+ end
45
+ stderr_reader = Thread.new do
46
+ while (chunk = child_err.readpartial(8192) rescue nil)
47
+ err << chunk
48
+ on_stderr&.call(chunk)
49
+ end
50
+ end
51
+ [writer, stdout_reader, stderr_reader].each(&:join)
52
+ status = wait.value
53
+ end
35
54
  RawResult.new(stdout: out, stderr: err, exit_code: status.exitstatus || 0)
36
55
  end
37
56
 
@@ -69,7 +69,7 @@ module Bsdkrun
69
69
  # @return [Sandbox]
70
70
  # @raise [SandboxNotFound]
71
71
  def get(id)
72
- row = list(all: true).find { |m| m.id == id || m.id.start_with?(id) }
72
+ row = list(all: true).find { |m| m.id == id || m.id.start_with?(id) || m.name == id }
73
73
  raise SandboxNotFound, id unless row
74
74
 
75
75
  new(row.id)
@@ -109,9 +109,11 @@ module Bsdkrun
109
109
  # @param cwd [String, nil] working directory (emulated via +sh -c 'cd …'+).
110
110
  # @param throw_on_error [Boolean] raise {CommandFailed} on a non-zero exit.
111
111
  # @param log_level [Integer] per-command bsdkrun log level.
112
+ # @param on_stdout [Proc, nil] called with each stdout chunk as it arrives.
113
+ # @param on_stderr [Proc, nil] called with each stderr chunk as it arrives.
112
114
  # @return [Result]
113
115
  def exec(command, args: [], env: {}, tty: false, stdin: nil, cwd: nil,
114
- throw_on_error: false, log_level: 0)
116
+ throw_on_error: false, log_level: 0, on_stdout: nil, on_stderr: nil)
115
117
  argv = command.is_a?(Array) ? command.dup : [command, *args]
116
118
 
117
119
  if cwd
@@ -124,7 +126,8 @@ module Bsdkrun
124
126
  env.each { |k, v| cli.push("-e", "#{k}=#{v}") }
125
127
  cli.push(@id, *argv)
126
128
 
127
- res = Process.run(cli, stdin: stdin, log_level: log_level)
129
+ res = Process.run(cli, stdin: stdin, log_level: log_level,
130
+ on_stdout: on_stdout, on_stderr: on_stderr)
128
131
  result = Result.new(
129
132
  stdout: res.stdout, stderr: res.stderr, exit_code: res.exit_code,
130
133
  command: "exec #{argv.join(' ')}"
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Bsdkrun
4
4
  # The bsdkrun Ruby SDK version.
5
- VERSION = "0.3.0"
5
+ VERSION = "0.3.2"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bsdkrun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tsiry Sandratraina
@@ -42,7 +42,7 @@ description: |
42
42
  BSD/Linux/unikernel microVMs programmatically: create sandboxes, exec commands, manage
43
43
  lifecycle, and wire up global networks, volumes and images.
44
44
  email:
45
- - tsiry.sndr@gmail.com
45
+ - tsiry.sndr@rocksky.app
46
46
  executables: []
47
47
  extensions: []
48
48
  extra_rdoc_files: []