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.



200
201
202
# File 'lib/kamal_backup/command.rb', line 200

def output
  @output
end

Class Method Details

.available?(name) ⇒ Boolean

Returns:

  • (Boolean)


210
211
212
213
214
215
# File 'lib/kamal_backup/command.rb', line 210

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



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/kamal_backup/command.rb', line 217

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

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



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/kamal_backup/command.rb', line 244

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



202
203
204
205
206
207
208
# File 'lib/kamal_backup/command.rb', line 202

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