Module: Kennel::Utils
- Defined in:
- lib/kennel/utils.rb
Defined Under Namespace
Classes: TeeIO
Constant Summary collapse
- COLORS =
{ red: 31, green: 32, yellow: 33, cyan: 36, magenta: 35, default: 0 }.freeze
Class Method Summary collapse
- .ask(question) ⇒ Object
- .capture_sh(command) ⇒ Object
- .capture_stderr ⇒ Object
- .capture_stdout ⇒ Object
- .color(color, text) ⇒ Object
- .natural_order(name) ⇒ Object
- .parallel(items, max: 10) ⇒ Object
- .path_to_url(path) ⇒ Object
- .presence(value) ⇒ Object
- .retry(*errors, times:) ⇒ Object
- .snake_case(string) ⇒ Object
- .strip_shell_control(text) ⇒ Object
- .tee_output ⇒ Object
Class Method Details
.ask(question) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/kennel/utils.rb', line 28 def ask(question) Kennel.err.printf color(:red, "#{question} - press 'y' to continue: ") begin STDIN.gets.chomp == "y" rescue Interrupt # do not show a backtrace if user decides to Ctrl+C here Kennel.err.print "\n" exit 1 end end |
.capture_sh(command) ⇒ Object
77 78 79 80 81 |
# File 'lib/kennel/utils.rb', line 77 def capture_sh(command) result = `#{command} 2>&1` raise "Command failed:\n#{command}\n#{result}" unless $CHILD_STATUS.success? result end |
.capture_stderr ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/kennel/utils.rb', line 55 def capture_stderr old = Kennel.err Kennel.err = StringIO.new yield Kennel.err.string ensure Kennel.err = old end |
.capture_stdout ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/kennel/utils.rb', line 46 def capture_stdout old = Kennel.out Kennel.out = StringIO.new yield Kennel.out.string ensure Kennel.out = old end |
.color(color, text) ⇒ Object
38 39 40 |
# File 'lib/kennel/utils.rb', line 38 def color(color, text) "\e[#{COLORS.fetch(color)}m#{text}\e[0m" end |
.natural_order(name) ⇒ Object
114 115 116 |
# File 'lib/kennel/utils.rb', line 114 def natural_order(name) name.split(/(\d+)/).each_with_index.map { |x, i| i.odd? ? x.to_i : x } end |
.parallel(items, max: 10) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/kennel/utils.rb', line 91 def parallel(items, max: 10) threads = [items.size, max].min work = items.each_with_index.to_a done = Array.new(items.size) workers = Array.new(threads).map do Thread.new do loop do item, i = work.pop break unless i done[i] = begin yield item rescue StandardError => e work.clear e end end end end workers.each(&:join) done.each { |d| raise d if d.is_a?(StandardError) } end |
.path_to_url(path) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/kennel/utils.rb', line 83 def path_to_url(path) if subdomain = ENV["DATADOG_SUBDOMAIN"] "https://#{subdomain}.datadoghq.com#{path}" else path end end |
.presence(value) ⇒ Object
24 25 26 |
# File 'lib/kennel/utils.rb', line 24 def presence(value) value.nil? || value.empty? ? nil : value end |
.retry(*errors, times:) ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/kennel/utils.rb', line 118 def retry(*errors, times:) yield rescue *errors => e times -= 1 raise if times < 0 Kennel.err.puts "Error #{e}, #{times} retries left" retry end |
.snake_case(string) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/kennel/utils.rb', line 17 def snake_case(string) string.gsub(/::/, "_") # Foo::Bar -> foo_bar .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') # FOOBar -> foo_bar .gsub(/([a-z\d])([A-Z])/, '\1_\2') # fooBar -> foo_bar .downcase end |
.strip_shell_control(text) ⇒ Object
42 43 44 |
# File 'lib/kennel/utils.rb', line 42 def strip_shell_control(text) text.gsub(/\e\[\d+m(.*?)\e\[0m/, "\\1").gsub(/.#{Regexp.escape("\b")}/, "") end |
.tee_output ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/kennel/utils.rb', line 64 def tee_output old_stdout = Kennel.out old_stderr = Kennel.err capture = StringIO.new Kennel.out = TeeIO.new([capture, Kennel.out]) Kennel.err = TeeIO.new([capture, Kennel.err]) yield capture.string ensure Kennel.out = old_stdout Kennel.err = old_stderr end |