kamal-backup 0.3.0.beta6 → 0.3.0.beta7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68d3201dc26c940938479dc76d1c0e17b206676ae69610d736a69879a78de21c
4
- data.tar.gz: dd58ee38da83d4652515b108869e5fe441a25d80697b7db3ea1c606240a44766
3
+ metadata.gz: 841b29d2ee8cc4598cb1880da4a7bf884cef141816432a9d960ff400b7535d25
4
+ data.tar.gz: 3784f1bfe50f1480f457bfb40a18e30ab5905db4a300a193fe5d68ac50d67f2f
5
5
  SHA512:
6
- metadata.gz: 37ca5f6463fe6eb76c2f6994a783e2180f3eecff24e834550743c1f9130e6ad3019ce1e296dc44ac523a36c065f1e05ac00aaaec7c7224254daea0c2d683d7a0
7
- data.tar.gz: '0985cd5e2eb8a52db5dcc4265271eddde1513c8c8db80d0aec21ee3ca1be0bd42e2adcd60ffeb942aa7bb2d926cbad69f5d89051485ce64145f3f5c821abe3bb'
6
+ metadata.gz: 9929349441963b8297254c10faa1446cf4f7602638a7637ab655ffcadb32d31d012ea8827890fc29175dfc4e344a5afc1edacbf960cfe86d6aec2a83c88385c6
7
+ data.tar.gz: fbfee00eae64424a92e5d039512ff51d01abecb6dd6a61d8d44ec97dc2e86208f06fd5c191947107a9c9adaa4da80a77a9eebabc9e9e12b96d7e60dcb0ce0e06
@@ -27,14 +27,49 @@ module KamalBackup
27
27
  CommandResult = Struct.new(:stdout, :stderr, :status, :streamed, keyword_init: true)
28
28
 
29
29
  class CommandOutput
30
- def initialize(io: $stdout)
30
+ LEVELS = {
31
+ "DEBUG" => 0,
32
+ "INFO" => 1,
33
+ "WARN" => 2,
34
+ "ERROR" => 3,
35
+ "FATAL" => 4
36
+ }.freeze
37
+ LEVEL_COLORS = {
38
+ "DEBUG" => :black,
39
+ "INFO" => :blue,
40
+ "WARN" => :yellow,
41
+ "ERROR" => :red,
42
+ "FATAL" => :red
43
+ }.freeze
44
+ COLOR_CODES = {
45
+ black: 30,
46
+ red: 31,
47
+ green: 32,
48
+ yellow: 33,
49
+ blue: 34,
50
+ magenta: 35,
51
+ cyan: 36,
52
+ white: 37,
53
+ light_black: 90,
54
+ light_red: 91,
55
+ light_green: 92,
56
+ light_yellow: 93,
57
+ light_blue: 94,
58
+ light_magenta: 95,
59
+ light_cyan: 96,
60
+ light_white: 97
61
+ }.freeze
62
+
63
+ def initialize(io: $stdout, env: ENV, verbosity: :info)
31
64
  @io = io
65
+ @env = env
66
+ @verbosity = LEVELS.fetch(verbosity.to_s.upcase)
32
67
  @mutex = Mutex.new
33
68
  @buffers = {}
34
69
  end
35
70
 
36
71
  def info(message, redactor:)
37
- write_line(" INFO #{redactor.redact_string(message)}")
72
+ write_message("INFO", redactor.redact_string(message))
38
73
  end
39
74
 
40
75
  def command_start(spec, redactor:)
@@ -42,8 +77,8 @@ module KamalBackup
42
77
  started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
43
78
  display = spec.display(redactor)
44
79
 
45
- write_line(" INFO [#{id}] Running #{display} locally")
46
- write_line(" DEBUG [#{id}] Command: #{display}")
80
+ write_message("INFO", "Running #{colorize(display, :yellow, :bold)} #{local_target}", id)
81
+ write_message("DEBUG", "Command: #{colorize(display, :blue)}", id)
47
82
 
48
83
  { id: id, started_at: started_at, redactor: redactor }
49
84
  end
@@ -51,6 +86,7 @@ module KamalBackup
51
86
  def command_output(context, _stream, data, redactor:)
52
87
  raw = data.to_s
53
88
  return if raw.empty?
89
+ return unless log_level?("DEBUG")
54
90
 
55
91
  synchronize do
56
92
  key = [context.fetch(:id), _stream]
@@ -62,16 +98,23 @@ module KamalBackup
62
98
  def command_exit(context, status)
63
99
  runtime = Process.clock_gettime(Process::CLOCK_MONOTONIC) - context.fetch(:started_at)
64
100
  result = status.to_i.zero? ? "successful" : "failed"
101
+ result_color = status.to_i.zero? ? :green : :red
65
102
 
66
103
  synchronize do
67
104
  flush_output_buffers(context)
68
- @io.puts(" INFO [#{context.fetch(:id)}] Finished in #{format("%.3f seconds", runtime)} with exit status #{status} (#{result}).")
105
+ write_message_unlocked("INFO", "Finished in #{format("%.3f seconds", runtime)} with exit status #{status} (#{colorize(result, result_color, :bold)}).", context.fetch(:id))
69
106
  end
70
107
  end
71
108
 
72
109
  private
73
- def write_line(message)
74
- synchronize { @io.puts(message) }
110
+ def write_message(level, message, id = nil)
111
+ return unless log_level?(level)
112
+
113
+ synchronize { write_message_unlocked(level, message, id) }
114
+ end
115
+
116
+ def write_message_unlocked(level, message, id = nil)
117
+ @io.puts(format_message(level, message, id)) if log_level?(level)
75
118
  end
76
119
 
77
120
  def synchronize(&block)
@@ -87,7 +130,7 @@ module KamalBackup
87
130
  end
88
131
 
89
132
  @buffers[key] = buffer
90
- write_output(context, output, redactor: redactor) unless output.empty?
133
+ write_output(context, output, redactor: redactor, stream: key.last) unless output.empty?
91
134
  end
92
135
 
93
136
  def flush_output_buffers(context)
@@ -98,17 +141,50 @@ module KamalBackup
98
141
  output = @buffers.delete(key)
99
142
  next if output.to_s.empty?
100
143
 
101
- write_output(context, output, redactor: context.fetch(:redactor))
102
- @io.puts unless output.end_with?("\n")
144
+ write_output(context, output, redactor: context.fetch(:redactor), stream: key.last)
103
145
  end
104
146
  end
105
147
 
106
- def write_output(context, output, redactor:)
148
+ def write_output(context, output, redactor:, stream: nil)
149
+ color = stream == :stderr ? :red : :green
150
+
107
151
  redactor.redact_string(output).each_line do |line|
108
- @io.print(" DEBUG [#{context.fetch(:id)}] \t#{line}")
152
+ write_message_unlocked("DEBUG", colorize("\t#{line}".chomp, color), context.fetch(:id))
109
153
  end
110
154
  @io.flush if @io.respond_to?(:flush)
111
155
  end
156
+
157
+ def format_message(level, message, id = nil)
158
+ message = "[#{colorize(id, :green)}] #{message}" if id
159
+ "#{colorize(level.rjust(6), LEVEL_COLORS.fetch(level))} #{message}"
160
+ end
161
+
162
+ def local_target
163
+ user = @env["USER"].to_s.empty? ? @env["USERNAME"].to_s : @env["USER"].to_s
164
+
165
+ if user.empty?
166
+ "on #{colorize("localhost", :blue)}"
167
+ else
168
+ "as #{colorize(user, :blue)}@#{colorize("localhost", :blue)}"
169
+ end
170
+ end
171
+
172
+ def log_level?(level)
173
+ LEVELS.fetch(level) >= @verbosity
174
+ end
175
+
176
+ def colorize(value, color, mode = nil)
177
+ string = value.to_s
178
+ return string unless colorize?
179
+ return string unless COLOR_CODES.key?(color)
180
+
181
+ prefix = mode == :bold ? "\e[1;" : "\e[0;"
182
+ "#{prefix}#{COLOR_CODES.fetch(color)};49m#{string}\e[0m"
183
+ end
184
+
185
+ def colorize?
186
+ @env["SSHKIT_COLOR"] || (@io.respond_to?(:tty?) && @io.tty?)
187
+ end
112
188
  end
113
189
 
114
190
  class Command
@@ -218,6 +218,7 @@ module KamalBackup
218
218
  spec = CommandSpec.new(argv: argv)
219
219
  options = {
220
220
  redactor: @redactor,
221
+ log: false,
221
222
  log_output: false,
222
223
  tee_stdout: stream ? @stdout : nil,
223
224
  tee_stderr: stream ? @stderr : nil
@@ -1,3 +1,3 @@
1
1
  module KamalBackup
2
- VERSION = "0.3.0.beta6"
2
+ VERSION = "0.3.0.beta7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kamal-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.beta6
4
+ version: 0.3.0.beta7
5
5
  platform: ruby
6
6
  authors:
7
7
  - crmne