Class: KamalBackup::Command
- Inherits:
-
Object
- Object
- KamalBackup::Command
- Defined in:
- lib/kamal_backup/command.rb
Class Attribute Summary collapse
-
.output ⇒ Object
Returns the value of attribute output.
Class Method Summary collapse
- .available?(name) ⇒ Boolean
- .capture(spec, input: nil, redactor:, log: true, log_output: true, tee_stdout: nil, tee_stderr: nil) ⇒ Object
- .collect_stream(io, command_output: self.output, context: nil, stream: :stdout, log_output: true, tee_io: nil, redactor: nil) ⇒ Object
- .with_output(output) ⇒ Object
Class Attribute Details
.output ⇒ Object
Returns the value of attribute output.
209 210 211 |
# File 'lib/kamal_backup/command.rb', line 209 def output @output end |
Class Method Details
.available?(name) ⇒ Boolean
219 220 221 222 223 224 |
# File 'lib/kamal_backup/command.rb', line 219 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
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/kamal_backup/command.rb', line 226 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
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/kamal_backup/command.rb', line 253 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
211 212 213 214 215 216 217 |
# File 'lib/kamal_backup/command.rb', line 211 def with_output(output) previous_output = self.output self.output = output yield ensure self.output = previous_output end |