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.



64
65
66
67
68
69
70
# File 'lib/kamal_backup/command.rb', line 64

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



107
108
109
110
111
112
113
114
115
116
# File 'lib/kamal_backup/command.rb', line 107

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)
    write_message_unlocked("INFO", "Finished in #{format("%.3f seconds", runtime)} with exit status #{status} (#{colorize(result, result_color, :bold)}).", context.fetch(:id))
  end
end

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



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kamal_backup/command.rb', line 95

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



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

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



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

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

#error(message, redactor:) ⇒ Object



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

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

#info(message, redactor:) ⇒ Object



72
73
74
# File 'lib/kamal_backup/command.rb', line 72

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