Module: GreenHat::ShellHelper

Defined in:
lib/greenhat/shell/filter_help.rb,
lib/greenhat/shell/log.rb,
lib/greenhat/shell/list.rb,
lib/greenhat/shell/page.rb,
lib/greenhat/shell/faststats.rb,
lib/greenhat/shell/color_string.rb,
lib/greenhat/shell/shell_helper.rb,
lib/greenhat/reports/shell_helper.rb

Overview

Common Helpers

Defined Under Namespace

Modules: Faststats, Filter, List, Log, Page, Reports, StringColor

Class Method Summary collapse

Class Method Details

.any_things?(file_list, flags = {}, base_list = nil) ⇒ Boolean

Shortcut to see if any things exist

Returns:

  • (Boolean)


316
317
318
# File 'lib/greenhat/shell/shell_helper.rb', line 316

def self.any_things?(file_list, flags = {}, base_list = nil)
  !find_things(file_list, flags, base_list).empty?
end

.common_optsObject

General Helper for show



363
364
365
366
367
368
369
370
371
372
373
# File 'lib/greenhat/shell/shell_helper.rb', line 363

def self.common_opts
  puts 'Common Options'.pastel(:blue)
  puts '  --raw'.pastel(:green)
  puts '    Do not use less/paging'
  puts

  puts '  --archive'.pastel(:green)
  puts '    Limit to specific archive name (inclusive). Matching SOS tar.gz name'
  puts '    Ex: --archive=dev-gitlab_20210622154626, --archive=202106,202107'
  puts
end

.create_ledger(data, flags) ⇒ Object

Collect Class Objects for output



86
87
88
89
90
# File 'lib/greenhat/shell/shell_helper.rb', line 86

def self.create_ledger(data, flags)
  data.map do |x|
    Paper.new(data: x, flags: flags)
  end
end

.entry_truncate(entry, truncate) ⇒ Object



214
215
216
217
218
219
220
221
222
223
# File 'lib/greenhat/shell/shell_helper.rb', line 214

def self.entry_truncate(entry, truncate)
  # Ignore if Truncation Off
  return entry if truncate.zero?

  # Only truncate large strings
  return entry unless entry.instance_of?(String) && entry.size > truncate

  # Include  '...' to indicate truncation
  "#{entry.to_s[0..truncate]} #{'...'.pastel(:bright_blue)}"
end

.field_table(list, columns: 4) ⇒ Object

5 Columns cause issues with resizing



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/greenhat/shell/shell_helper.rb', line 259

def self.field_table(list, columns: 4)
  return nil if list.empty?

  # Split list into groups based on columns
  groups = list.each_slice((list.size / columns.to_f).ceil).to_a

  # Max Width for Table
  truncate = (TTY::Screen.width - groups.count) / groups.count

  # Process Truncation
  groups.each do |group|
    group.map! { |f| f[0..truncate]&.to_sym }
  end

  table = TTY::Table.new do |t|
    loop do
      break if groups.all?(&:empty?)

      t << groups.map(&:shift)
    end
  end

  table.render(:unicode, padding: [0, 1, 0, 1], resize: false)
end

.fields_print(files) ⇒ Object

Table Printer Helper



248
249
250
251
252
253
254
255
256
# File 'lib/greenhat/shell/shell_helper.rb', line 248

def self.fields_print(files)
  fields = ShellHelper.find_things(files).map(&:fields).flatten.uniq

  # truncate = (TTY::Screen.width - columns) / columns
  # fields.map! { |f| f[0..truncate] }
  puts ShellHelper.field_table(fields)

  # ShellHelper.fields_print(files)
end

.file_output(files, flags = {}) ⇒ Object

Use File Process for Output



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/greenhat/shell/shell_helper.rb', line 5

def self.file_output(files, flags = {})
  results = file_process(files) do |file|
    [
      file.friendly_name,
      file.output(false),
      "\n"
    ]
  end

  ShellHelper.show(results.flatten, flags)
end

.file_process(files, &block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/greenhat/shell/shell_helper.rb', line 17

def self.file_process(files, &block)
  files.map do |file|
    next if file.output(false).empty?

    block.call(file)
  end.flatten
end

.files(file_list, base_list = nil, flags = {}) ⇒ Object

Unified Files Interface



285
286
287
288
289
290
291
292
293
# File 'lib/greenhat/shell/shell_helper.rb', line 285

def self.files(file_list, base_list = nil, flags = {})
  base_list ||= Thing.all

  # Prepare Log List
  file_list = prepare_list(file_list, base_list)

  # Convert to Things
  find_things(file_list, flags, base_list)
end

.filter_internal(search = '') ⇒ Object

Internal Query Helper query = ‘gitlab-rails/application_json.log –message!=“Cannot obtain an exclusive lease” –severity=error’ ShellHelper.filter_internal(query)



204
205
206
207
208
209
210
211
212
# File 'lib/greenhat/shell/shell_helper.rb', line 204

def self.filter_internal(search = '')
  files, flags, args = Args.parse(Shellwords.split(search))
  flags[:combine] = true

  # Default to everything
  files = Thing.all.map(&:name) if files.empty?

  Query.start(files, flags, args)
end

.find_things(file_list, flags = {}, base_list = nil) ⇒ Object

Shortcut find things



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/greenhat/shell/shell_helper.rb', line 321

def self.find_things(file_list, flags = {}, base_list = nil)
  base_list ||= Thing.all

  things = file_list.uniq.flat_map do |file|
    # If Thing, Return Thing
    return file if file.instance_of?(Thing)

    if flags.fuzzy_file_match
      base_list.select { |x| x.name.include?(file) || x.type.include?(file) }
    else
      Thing.where name: file
    end
  end.uniq

  # Host / Archive
  things.select! { |x| x.archive? flags.archive } if flags.key?(:archive)

  things
end

.human_size_to_number(string) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/greenhat/shell/shell_helper.rb', line 343

def self.human_size_to_number(string)
  size, unit = string.scan(/(\d*\.?\d+)\s?(Bytes?|KB|MB|GB|TB)/i).first
  number = size.to_f

  number = case unit.downcase
           when 'byte', 'bytes'
             number
           when 'kb'
             number * 1024
           when 'mb'
             number * 1024 * 1024
           when 'gb'
             number * 1024 * 1024 * 1024
           when 'tb'
             number * 1024 * 1024 * 1024 * 1024
           end
  number.round
end

.page(data) ⇒ Object

Pagination Helper



26
27
28
29
30
31
32
# File 'lib/greenhat/shell/shell_helper.rb', line 26

def self.page(data)
  TTY::Pager.page do |pager|
    data.flatten.each do |output|
      pager.write("\n#{output}") # write line to the pager
    end
  end
end

.prepare_list(log_list, base_list = nil) ⇒ Object

Total Log List Manipulator



296
297
298
299
300
301
302
303
304
305
306
# File 'lib/greenhat/shell/shell_helper.rb', line 296

def self.prepare_list(log_list, base_list = nil)
  base_list ||= Thing.list

  # Assume all
  log_list.push '*' if log_list.empty?

  # Map for All
  log_list = base_list.map(&:name) if log_list == ['*']

  log_list
end

.puts(text = '') ⇒ Object



92
93
94
# File 'lib/greenhat/shell/shell_helper.rb', line 92

def self.puts(text = '')
  $stdout.puts text
end

.show(data, flags = {}) ⇒ Object

Show Data / Auto Paginate Helper



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/greenhat/shell/shell_helper.rb', line 35

def self.show(data, flags = {})
  # If Block of String
  if data.instance_of?(String)
    TTY::Pager.page data
    return true
  end

  # If raw just print out
  if flags[:raw]
    puts data.join("\n")
    return true
  end

  # Check if content needs to paged, or if auto_height is off
  if Page.skip?(flags, data)
    create_ledger(data, flags).each { |entry| puts entry.render }
    return true
  end

  # Create Jobs
  # Generate List of Formatted Output
  ledger = create_ledger(data, flags)

  # Start Thread
  # TODO: Confirm / Test or Remove
  unless flags.no_paper_thread
    thread = Thread.new do
      pending = ledger.clone
      loop do
        pending = pending.reject(&:done)
        break if pending.empty?

        pending.first.async
      end
    end
  end

  TTY::Pager.page do |pager|
    ledger.each do |paper|
      # New Line is only needed on pager.write
      pager.write "#{paper.render}\n"
    end
  end

  # Terminate
  thread.kill unless flags.no_paper_thread

  # -----------------
end

.thing_listObject

Fuzzy match for things / List used for processable (Hash Entries)



309
310
311
312
313
# File 'lib/greenhat/shell/shell_helper.rb', line 309

def self.thing_list
  @thing_list ||= Thing.list.map(&:name)

  @thing_list
end

.total_count(results, flags) ⇒ Object

Total Count Helper



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/greenhat/shell/shell_helper.rb', line 226

def self.total_count(results, flags)
  # Option to only return the count
  return results.count if flags[:total] == 'simple'

  output = {}
  output[:total] = results.count
  output[:duration] = Query.calculate_duration(results)

  # Sort / Get first and Last
  list = results.select(&:time).map(&:time)
  unless list.blank?
    output[:start] = list.first
    output[:end] = list.last
  end

  # Hide empty results
  output.reject! { |_k, v| v.blank? }

  [output]
end