Class: KamalBackup::CommandOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/kamal_backup/command.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.



67
68
69
70
71
72
73
# File 'lib/kamal_backup/command.rb', line 67

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



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kamal_backup/command.rb', line 110

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



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/kamal_backup/command.rb', line 98

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



87
88
89
90
91
92
93
94
95
96
# File 'lib/kamal_backup/command.rb', line 87

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



83
84
85
# File 'lib/kamal_backup/command.rb', line 83

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

#error(message, redactor:) ⇒ Object



79
80
81
# File 'lib/kamal_backup/command.rb', line 79

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

#info(message, redactor:) ⇒ Object



75
76
77
# File 'lib/kamal_backup/command.rb', line 75

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