Class: KamalBackup::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/kamal_backup/command.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.outputObject

Returns the value of attribute output.



214
215
216
# File 'lib/kamal_backup/command.rb', line 214

def output
  @output
end

Class Method Details

.available?(name) ⇒ Boolean

Returns:

  • (Boolean)


224
225
226
227
228
229
# File 'lib/kamal_backup/command.rb', line 224

def available?(name)
  ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).any? do |dir|
    path = File.join(dir, name)
    File.executable?(path) && !File.directory?(path)
  end
end

.capture(spec, input: nil, redactor:, log: true, log_output: true, tee_stdout: nil, tee_stderr: nil) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/kamal_backup/command.rb', line 231

def capture(spec, input: nil, redactor:, log: true, log_output: true, tee_stdout: nil, tee_stderr: nil)
  output = log ? self.output : nil
  return capture_quietly(spec, input: input, redactor: redactor) unless output || tee_stdout || tee_stderr

  context = output&.command_start(spec, redactor: redactor)
  stdout, stderr, status = popen_capture(
    spec,
    input: input,
    redactor: redactor,
    output: output,
    context: context,
    log_output: log_output,
    tee_stdout: tee_stdout,
    tee_stderr: tee_stderr
  )
  output&.command_exit(context, status.exitstatus)
  result = CommandResult.new(stdout: stdout, stderr: stderr, status: status.exitstatus, streamed: !!(tee_stdout || tee_stderr))

  if status.success?
    result
  else
    raise command_failure(spec, status.exitstatus, stdout, stderr, redactor)
  end
rescue Errno::ENOENT => e
  raise command_not_found(spec, e)
end

.capture_pty(spec, redactor:, tee_stdout: nil) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/kamal_backup/command.rb', line 258

def capture_pty(spec, redactor:, tee_stdout: nil)
  output = +""
  tee_buffer = +""
  result = nil

  PTY.spawn(spec.env, *spec.argv) do |reader, writer, pid|
    writer.close

    begin
      loop do
        chunk = reader.readpartial(16 * 1024)
        output << chunk
        tee_buffer = tee_stream(tee_stdout, redactor, tee_buffer, chunk) if tee_stdout
      end
    rescue EOFError, Errno::EIO
      flush_tee_stream(tee_stdout, redactor, tee_buffer) if tee_stdout
    ensure
      reader.close unless reader.closed?
    end

    _, status = Process.wait2(pid)
    result = CommandResult.new(stdout: output, stderr: "", status: status.exitstatus, streamed: !!tee_stdout)

    unless status.success?
      raise command_failure(spec, status.exitstatus, output, output, redactor)
    end
  end

  result
rescue Errno::ENOENT => e
  raise command_not_found(spec, e)
end

.collect_stream(io, command_output: self.output, context: nil, stream: :stdout, log_output: true, tee_io: nil, redactor: nil) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/kamal_backup/command.rb', line 291

def collect_stream(io, command_output: self.output, context: nil, stream: :stdout, log_output: true, tee_io: nil, redactor: nil)
  captured_output = +""
  tee_buffer = +""

  loop do
    chunk = io.readpartial(16 * 1024)
    captured_output << chunk
    command_output&.command_output(context, stream, chunk, redactor: redactor) if log_output && context
    tee_buffer = tee_stream(tee_io, redactor, tee_buffer, chunk) if tee_io
  rescue EOFError
    flush_tee_stream(tee_io, redactor, tee_buffer) if tee_io
    break
  end

  captured_output
end

.with_output(output) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/kamal_backup/command.rb', line 216

def with_output(output)
  previous_output = self.output
  self.output = output
  yield
ensure
  self.output = previous_output
end