module Kennel::Utils
Constants
- COLORS
Public Class Methods
all_keys(items)
click to toggle source
# File lib/kennel/utils.rb, line 144 def all_keys(items) case items when Hash then items.keys + items.values.flat_map { |v| all_keys(v) } when Array then items.flat_map { |i| all_keys(i) } else [] end end
ask(question)
click to toggle source
# File lib/kennel/utils.rb, line 44 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)
click to toggle source
# File lib/kennel/utils.rb, line 93 def capture_sh(command) result = `#{command} 2>&1` raise "Command failed:\n#{command}\n#{result}" unless $CHILD_STATUS.success? result end
capture_stderr() { || ... }
click to toggle source
# File lib/kennel/utils.rb, line 71 def capture_stderr old = Kennel.err Kennel.err = StringIO.new yield Kennel.err.string ensure Kennel.err = old end
capture_stdout() { || ... }
click to toggle source
# File lib/kennel/utils.rb, line 62 def capture_stdout old = Kennel.out Kennel.out = StringIO.new yield Kennel.out.string ensure Kennel.out = old end
color(color, text)
click to toggle source
# File lib/kennel/utils.rb, line 54 def color(color, text) "\e[#{COLORS.fetch(color)}m#{text}\e[0m" end
natural_order(name)
click to toggle source
# File lib/kennel/utils.rb, line 130 def natural_order(name) name.split(/(\d+)/).each_with_index.map { |x, i| i.odd? ? x.to_i : x } end
parallel(items, max: 10) { |item| ... }
click to toggle source
# File lib/kennel/utils.rb, line 107 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
parameterize(string)
click to toggle source
simplified version of apidock.com/rails/ActiveSupport/Inflector/parameterize
# File lib/kennel/utils.rb, line 32 def parameterize(string) string .downcase .gsub(/[^a-z0-9\-_]+/, "-") # remove unsupported .gsub(/-{2,}/, "-") # remove duplicates .gsub(/^-|-$/, "") # remove leading/trailing end
path_to_url(path)
click to toggle source
# File lib/kennel/utils.rb, line 99 def path_to_url(path) if subdomain = ENV["DATADOG_SUBDOMAIN"] "https://#{subdomain}.datadoghq.com#{path}" else path end end
presence(value)
click to toggle source
# File lib/kennel/utils.rb, line 40 def presence(value) value.nil? || value.empty? ? nil : value end
pretty_inspect(object)
click to toggle source
TODO: use awesome-print or similar, but it has too many monkey-patches github.com/amazing-print/amazing_print/issues/36
# File lib/kennel/utils.rb, line 154 def pretty_inspect(object) string = object.inspect string.gsub!(/:([a-z_]+)=>/, "\\1: ") 10.times do string.gsub!(/{(\S.*?\S)}/, "{ \\1 }") || break end string end
retry(*errors, times:) { || ... }
click to toggle source
# File lib/kennel/utils.rb, line 134 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)
click to toggle source
# 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 .tr("-", "_") # foo-bar -> foo_bar .downcase end
strip_shell_control(text)
click to toggle source
# File lib/kennel/utils.rb, line 58 def strip_shell_control(text) text.gsub(/\e\[\d+m(.*?)\e\[0m/, "\\1").gsub(/.#{Regexp.escape("\b")}/, "") end
tee_output() { || ... }
click to toggle source
# File lib/kennel/utils.rb, line 80 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
title_case(string)
click to toggle source
for child projects, not used internally
# File lib/kennel/utils.rb, line 27 def title_case(string) string.split(/[\s_]/).map(&:capitalize) * " " end