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.



219
220
221
# File 'lib/kamal_backup/command.rb', line 219

def output
  @output
end

Class Method Details

.available?(name) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
232
233
234
# File 'lib/kamal_backup/command.rb', line 229

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, redactor:, input: nil, log: true, log_output: true, tee_stdout: nil, tee_stderr: nil) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/kamal_backup/command.rb', line 236

def capture(spec, redactor:, input: nil, 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).nil?)

  raise command_failure(spec, status.exitstatus, stdout, stderr, redactor) unless status.success?

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

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



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
290
291
# File 'lib/kamal_backup/command.rb', line 262

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.nil?)

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

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

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



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

def collect_stream(io, command_output: 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



221
222
223
224
225
226
227
# File 'lib/kamal_backup/command.rb', line 221

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