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

Class Method Details

.all_keys(items) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/kennel/utils.rb', line 139

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) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/kennel/utils.rb', line 44

def ask(question)
  Kennel.err.printf color(:red, "#{question} -  press 'y' to continue: ", force: true)
  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



91
92
93
94
95
# File 'lib/kennel/utils.rb', line 91

def capture_sh(command)
  result = `#{command} 2>&1`
  raise "Command failed:\n#{command}\n#{result}" unless $CHILD_STATUS.success?
  result
end

.capture_stderrObject



69
70
71
72
73
74
75
76
# File 'lib/kennel/utils.rb', line 69

def capture_stderr
  old = Kennel.err
  Kennel.err = StringIO.new
  yield
  Kennel.err.string
ensure
  Kennel.err = old
end

.capture_stdoutObject



60
61
62
63
64
65
66
67
# File 'lib/kennel/utils.rb', line 60

def capture_stdout
  old = Kennel.out
  Kennel.out = StringIO.new
  yield
  Kennel.out.string
ensure
  Kennel.out = old
end

.color(color, text, force: false) ⇒ Object



54
55
56
57
58
# File 'lib/kennel/utils.rb', line 54

def color(color, text, force: false)
  return text unless force || Kennel.out.tty?

  "\e[#{COLORS.fetch(color)}m#{text}\e[0m"
end

.natural_order(name) ⇒ Object



125
126
127
# File 'lib/kennel/utils.rb', line 125

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/kennel/utils.rb', line 102

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 Exception => e # rubocop:disable Lint/RescueException
            work.clear
            e
          end
      end
    end
  end
  workers.each(&:join)
  done.each { |d| raise d if d.is_a?(Exception) }
end

.parameterize(string) ⇒ Object



32
33
34
35
36
37
38
# 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, subdomain: nil) ⇒ Object



97
98
99
100
# File 'lib/kennel/utils.rb', line 97

def path_to_url(path, subdomain: nil)
  subdomain ||= (ENV["DATADOG_SUBDOMAIN"] || "app")
  "https://#{subdomain}.datadoghq.com#{path}"
end

.presence(value) ⇒ Object



40
41
42
# File 'lib/kennel/utils.rb', line 40

def presence(value)
  value.nil? || value.empty? ? nil : value
end

.pretty_inspect(object) ⇒ Object

TODO: use awesome-print or similar, but it has too many monkey-patches github.com/amazing-print/amazing_print/issues/36



149
150
151
152
153
154
155
156
# File 'lib/kennel/utils.rb', line 149

def pretty_inspect(object)
  string = object.inspect.dup
  string.gsub!(/:([a-z_]+)=>/, "\\1: ")
  10.times do
    string.gsub!(/{(\S.*?\S)}/, "{ \\1 }") || break
  end
  string
end

.retry(*errors, times:) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/kennel/utils.rb', line 129

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
23
24
# 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

.tee_outputObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kennel/utils.rb', line 78

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) ⇒ Object

for child projects, not used internally



27
28
29
# File 'lib/kennel/utils.rb', line 27

def title_case(string)
  string.split(/[\s_]/).map(&:capitalize) * " "
end