Class: KamalBackup::CommandOutput

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

Constant Summary collapse

LEVELS =
{
  'DEBUG' => 0,
  'INFO' => 1,
  'WARN' => 2,
  'ERROR' => 3,
  'FATAL' => 4
}.freeze
LEVEL_COLORS =
{
  'DEBUG' => :black,
  'INFO' => :blue,
  'WARN' => :yellow,
  'ERROR' => :red,
  'FATAL' => :red
}.freeze
COLOR_CODES =
{
  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37,
  light_black: 90,
  light_red: 91,
  light_green: 92,
  light_yellow: 93,
  light_blue: 94,
  light_magenta: 95,
  light_cyan: 96,
  light_white: 97
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(io: $stdout, env: ENV, verbosity: :info) ⇒ CommandOutput

Returns a new instance of CommandOutput.



40
41
42
43
44
45
46
# File 'lib/kamal_backup/command_output.rb', line 40

def initialize(io: $stdout, env: ENV, verbosity: :info)
  @io = io
  @env = env
  @verbosity = LEVELS.fetch(verbosity.to_s.upcase)
  @mutex = Mutex.new
  @buffers = {}
end

Instance Method Details

#command_exit(context, status) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kamal_backup/command_output.rb', line 83

def command_exit(context, status)
  runtime = Process.clock_gettime(Process::CLOCK_MONOTONIC) - context.fetch(:started_at)
  result = status.to_i.zero? ? 'successful' : 'failed'
  result_color = status.to_i.zero? ? :green : :red

  synchronize do
    flush_output_buffers(context)
    message = "Finished in #{format('%.3f seconds', runtime)} with exit status #{status} " \
              "(#{colorize(result, result_color, :bold)})."
    write_message_unlocked('INFO', message, context.fetch(:id))
  end
end

#command_output(context, stream, data, redactor:) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kamal_backup/command_output.rb', line 71

def command_output(context, stream, data, redactor:)
  raw = data.to_s
  return if raw.empty?
  return unless log_level?('DEBUG')

  synchronize do
    key = [context.fetch(:id), stream]
    @buffers[key] = "#{@buffers[key]}#{raw}"
    flush_complete_output_lines(context, key, redactor: redactor)
  end
end

#command_start(spec, redactor:) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/kamal_backup/command_output.rb', line 60

def command_start(spec, redactor:)
  id = SecureRandom.hex(4)
  started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  display = spec.display(redactor)

  write_message('INFO', "Running #{colorize(display, :yellow, :bold)} #{target_for(spec)}", id)
  write_message('DEBUG', "Command: #{colorize(display, :blue)}", id)

  { id: id, started_at: started_at, redactor: redactor }
end

#decorate(value, color, mode = nil) ⇒ Object



56
57
58
# File 'lib/kamal_backup/command_output.rb', line 56

def decorate(value, color, mode = nil)
  colorize(value, color, mode)
end

#error(message, redactor:) ⇒ Object



52
53
54
# File 'lib/kamal_backup/command_output.rb', line 52

def error(message, redactor:)
  write_message('ERROR', colorize(redactor.redact_string(message), :red, :bold))
end

#info(message, redactor:) ⇒ Object



48
49
50
# File 'lib/kamal_backup/command_output.rb', line 48

def info(message, redactor:)
  write_message('INFO', redactor.redact_string(message))
end