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.



116
117
118
# File 'lib/kamal_backup/command.rb', line 116

def output
  @output
end

Class Method Details

.available?(name) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
# File 'lib/kamal_backup/command.rb', line 126

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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/kamal_backup/command.rb', line 133

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/kamal_backup/command.rb', line 160

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



118
119
120
121
122
123
124
# File 'lib/kamal_backup/command.rb', line 118

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