greenhat 0.5.0 → 0.6.2

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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/lib/greenhat/accessors/disk.rb +1 -1
  3. data/lib/greenhat/archive.rb +2 -0
  4. data/lib/greenhat/cli.rb +12 -2
  5. data/lib/greenhat/entrypoint.rb +36 -33
  6. data/lib/greenhat/host.rb +1 -1
  7. data/lib/greenhat/logbot.rb +1 -1
  8. data/lib/greenhat/paper/flag_helper.rb +18 -0
  9. data/lib/greenhat/paper/paper_helper.rb +118 -0
  10. data/lib/greenhat/paper.rb +34 -0
  11. data/lib/greenhat/reports/builder.rb +98 -0
  12. data/lib/greenhat/reports/helpers.rb +101 -0
  13. data/lib/greenhat/reports/internal_methods.rb +156 -0
  14. data/lib/greenhat/reports/reports/errors.rb +51 -0
  15. data/lib/greenhat/reports/reports/faststats.rb +42 -0
  16. data/lib/greenhat/reports/reports/full.rb +143 -0
  17. data/lib/greenhat/reports/runner.rb +58 -0
  18. data/lib/greenhat/reports/shared.rb +37 -0
  19. data/lib/greenhat/reports/shell_helper.rb +34 -0
  20. data/lib/greenhat/reports.rb +79 -0
  21. data/lib/greenhat/settings.rb +6 -1
  22. data/lib/greenhat/shell/args.rb +9 -9
  23. data/lib/greenhat/shell/color_string.rb +1 -1
  24. data/lib/greenhat/shell/faststats.rb +24 -5
  25. data/lib/greenhat/shell/field_helper.rb +1 -1
  26. data/lib/greenhat/shell/filter_help.rb +36 -189
  27. data/lib/greenhat/shell/log.rb +28 -16
  28. data/lib/greenhat/shell/markdown.rb +355 -352
  29. data/lib/greenhat/shell/process.rb +11 -5
  30. data/lib/greenhat/shell/query.rb +184 -28
  31. data/lib/greenhat/shell/report.rb +415 -412
  32. data/lib/greenhat/shell/reports.rb +41 -0
  33. data/lib/greenhat/shell/shell_helper.rb +172 -117
  34. data/lib/greenhat/shell.rb +13 -2
  35. data/lib/greenhat/thing/file_types.rb +38 -2
  36. data/lib/greenhat/thing/formatters/clean_raw.rb +1 -1
  37. data/lib/greenhat/thing/formatters/exporters.rb +48 -0
  38. data/lib/greenhat/thing/formatters/identify_db.rb +32 -0
  39. data/lib/greenhat/thing/formatters/runner_log.rb +70 -0
  40. data/lib/greenhat/thing/formatters/table.rb +15 -1
  41. data/lib/greenhat/thing/formatters/time_json.rb +12 -1
  42. data/lib/greenhat/thing/kind.rb +1 -1
  43. data/lib/greenhat/thing.rb +1 -0
  44. data/lib/greenhat/version.rb +1 -1
  45. data/lib/greenhat.rb +6 -8
  46. metadata +33 -4
  47. data/lib/greenhat/pry_helpers.rb +0 -51
  48. data/lib/greenhat/thing/super_log.rb +0 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 98d1acd7b3c698127214711199399e78d7b72f4762d8beb575fb2ac1d3e2ae1b
4
- data.tar.gz: 011d2f2e27b278a06f87605d13809f3472b5223f39937e6fed55ecc28476ea00
3
+ metadata.gz: 585d6d2f1488ca970ca64230991151e3417c28f14662dfdb6f02fec7c617d54a
4
+ data.tar.gz: 2999efd11049fc2d20ad1f5f1e8967b7b0d5caccce2f819530de9f0982349ed6
5
5
  SHA512:
6
- metadata.gz: 2b253023196cc698fc9022455e1a43e6add8fae2e63512e946fb207619e7e29c83783e9b9a265e1f77ce3dcbcb81be270b7bb3896a0822c6bad06c51dec8571f
7
- data.tar.gz: 850d05cc4ea5d95e53e289c4781232af8719286c9b3c1165cf9afeac25bf19122a79249bc290ddad9cc4b00767d4c5396052fe5c7e9c43b9b7742f9c15eb1c7f
6
+ metadata.gz: 21f480e7eda0513557b373283119317bd2e13ffcb6d8f97924fed30b03b4913ea7f86fee4b007e068384d3615cf18fd9eed405ce89df7c7bcb54eb6d36282a25
7
+ data.tar.gz: 3028f8003fdee07c04a0e327b2eca2138147203854bc8fbadd3784d2af80ea3660bb402d096632f25033f70cea456204aec5f0508eec2233c91f4099138a9d87
@@ -21,7 +21,7 @@ module GreenHat
21
21
  end
22
22
 
23
23
  def self.df(args = {})
24
- things = Thing.where(name: 'df_h')
24
+ things = Thing.where(name: 'df_hT')
25
25
 
26
26
  # Host / Archive
27
27
  things.select! { |x| x.archive? args.archive } if args.archive
@@ -62,7 +62,9 @@ module GreenHat
62
62
  when '.gz'
63
63
  `gzip -d "#{archive_path}"`
64
64
  when '.zip'
65
+ base_path = archive_path.gsub(File.basename(archive_path), '')
65
66
  `unzip -o -d "#{base_path}" #{archive_path}`
67
+ FileUtils.rm(archive_path)
66
68
  when '.bz2'
67
69
  `bzip2 -d #{archive_path}`
68
70
  when '.s'
data/lib/greenhat/cli.rb CHANGED
@@ -35,8 +35,14 @@ module GreenHat
35
35
  @reader ||= reader_setup
36
36
  end
37
37
 
38
+ # Context matters on which ones to autocomplete / Not saving in memory to allow better tab / auto complete
38
39
  def self.files
39
- @files ||= Thing.all.map(&:name).uniq
40
+ case current_location
41
+ when GreenHat::Shell::Log
42
+ Thing.all.select(&:log).map(&:name.uniq)
43
+ else
44
+ Thing.all.map(&:name).uniq
45
+ end
40
46
  end
41
47
 
42
48
  def self.reader_setup
@@ -314,6 +320,11 @@ module GreenHat
314
320
  @location = [spot]
315
321
  end
316
322
 
323
+ # Shell Context Location
324
+ def self.move_shell(spot)
325
+ @location = [GreenHat::Shell, spot]
326
+ end
327
+
317
328
  # Append to Location
318
329
  def self.submodule!
319
330
  spot = @list.shift
@@ -332,7 +343,6 @@ module GreenHat
332
343
  # Reader to Get Last Location's Available Methods / Keep Helper Methods
333
344
  def self.current_methods
334
345
  current_location.methods(false).map(&:to_s)
335
- # .reject { |x| x.include? '_help' }
336
346
  end
337
347
 
338
348
  def self.current_submodules
@@ -4,23 +4,25 @@ module GreenHat
4
4
  # Command Line Args / File Parsing etc
5
5
  module Entrypoint
6
6
  # If no arguments Supplied Print and quit - rather than nasty exception
7
- def self.start(raw)
8
- @clear = true # Reset screen by default
7
+ def self.start(raw, clear = true, cli = true)
8
+ @clear = clear # Reset screen by default
9
9
  Settings.start
10
10
  files, flags, args = Args.parse(raw)
11
11
  pre_args(flags, files)
12
12
  load_files files
13
13
  Cli.run_command(args) if args.any? { |arg| Cli.run_command?(arg) }
14
+
15
+ report_runner(args: args, files: files, raw: raw, flags: flags)
14
16
  post_args(flags)
15
17
  post_setup(flags)
16
18
 
17
- Cli.start # Kick over to CLI
19
+ Cli.start if cli # Kick over to CLI
18
20
  end
19
21
 
20
22
  # Arguments before general processing
21
23
  def self.pre_args(flags, files)
22
24
  # Help
23
- if flags?(%i[help h], flags)
25
+ if flags?(%i[help h], flags) && !ENV['GREENHAT_REPORT']
24
26
  cli_help
25
27
  exit 0
26
28
  end
@@ -53,30 +55,37 @@ module GreenHat
53
55
  end
54
56
  end
55
57
 
58
+ # Helper to allow running any kind of report with arguments directly with greenhat
59
+ def self.report_runner(args:, files:, raw:, flags:)
60
+ if flags.report || flags.r
61
+ GreenHat::Shell::Reports.default(['full'])
62
+ exit 0
63
+ end
64
+
65
+ # See if Param was passed
66
+ report = args.find { |x| %i[report r].include? x.field }
67
+ return false unless report
68
+
69
+ Cli.quiet! # Hush
70
+
71
+ # Filter for additional params for Reports
72
+ raw_filter = raw.reject do |entry|
73
+ entry.include?('--report') || files.include?(entry)
74
+ end
75
+
76
+ # Prepend report name
77
+ raw_filter.unshift(report.value)
78
+
79
+ GreenHat::Shell::Reports.default([report.value])
80
+
81
+ exit 0
82
+ end
83
+
56
84
  # Arguments to be handled after general processing
57
85
  def self.post_args(flags)
58
86
  # Supress Color
59
87
  GreenHat::Settings.settings[:color] = false if flags?(%i[no-color n], flags)
60
-
61
- # Run report and exit / Don't Clear for reports
62
- if flags?(%i[report r], flags)
63
- Cli.quiet!
64
- # Don't Use Pagination
65
- GreenHat::Shell.report(['--raw'])
66
- exit 0
67
- elsif flags?(%i[full-report f], flags)
68
- Cli.quiet!
69
- # Don't Use Pagination
70
- GreenHat::Shell.report(['--raw', '--full'])
71
- exit 0
72
- elsif flags?(%i[markdown m], flags)
73
- Cli.quiet!
74
- # Don't Use Pagination
75
- GreenHat::Shell.markdown_report(['--raw'])
76
- exit 0
77
- elsif @clear
78
- Cli.clear_screen
79
- end
88
+ Cli.clear_screen if @clear
80
89
  end
81
90
 
82
91
  def self.post_setup(flags)
@@ -116,15 +125,9 @@ module GreenHat
116
125
 
117
126
  puts 'Options'.pastel(:yellow)
118
127
  puts ' --report, -r'.pastel(:green)
119
- puts ' Run `report` against archives and exit'
120
- puts
121
-
122
- puts ' --full-report, -f'.pastel(:green)
123
- puts ' Run `report` with all details. e.g. include fast-stats top/errors'
124
- puts
125
-
126
- puts ' --markdown, -m'.pastel(:green)
127
- puts ' Run `markdown_report` against archives and exit'
128
+ puts ' Run default or specified `report` against archives and exit'
129
+ puts ' Default report "full", --report, --report=demo, --report=errors'
130
+ puts ' See reportable.md/wiki or the `reports` submodule'
128
131
  puts
129
132
 
130
133
  puts ' --quiet, -r'.pastel(:green)
data/lib/greenhat/host.rb CHANGED
@@ -115,7 +115,7 @@ class Host < Teron
115
115
  end
116
116
 
117
117
  def df_h
118
- file = find_thing 'df_h'
118
+ file = find_thing 'df_hT'
119
119
  return nil unless file
120
120
 
121
121
  file.raw
@@ -21,7 +21,7 @@ module LogBot
21
21
  end
22
22
  end
23
23
 
24
- puts output unless ENV['TESTING'] || GreenHat::Cli.quiet
24
+ puts output unless ENV.fetch('TESTING') { GreenHat::Cli.quiet }
25
25
  end
26
26
 
27
27
  def self.color_level(level)
@@ -0,0 +1,18 @@
1
+ module GreenHat
2
+ # Helpers for Paper Render
3
+ module PaperFlagHelper
4
+ # --------------------------------------------------------------------------
5
+ def truncate
6
+ flags[:truncate]
7
+ end
8
+
9
+ def table_style
10
+ flags[:table_style]&.to_sym || :unicode
11
+ end
12
+
13
+ def stats
14
+ flags[:stats]
15
+ end
16
+ # --------------------------------------------------------------------------
17
+ end
18
+ end
@@ -0,0 +1,118 @@
1
+ module GreenHat
2
+ # Helpers for Paper Render
3
+ module PaperHelpers
4
+ # =========================================================================
5
+ # Entrypoint
6
+ # =========================================================================
7
+ def process
8
+ LogBot.debug('Entry Show', data.class) if ENV['DEBUG']
9
+ case data
10
+ when Hash then render_table
11
+ when Float, Integer
12
+ self.output = format_table_entry(data)
13
+ # Ignore Special Formatting for Strings / Usually already formatted
14
+ when String
15
+ self.output = data
16
+ when Array
17
+ # Check for Nested Arrays (Array Table)
18
+ if data.first.instance_of? Array
19
+ render_array_table(data)
20
+ else
21
+ self.output = data
22
+ end
23
+ else
24
+ LogBot.warn('Shell Show', "Unknown #{data.class}")
25
+ self.output = data
26
+ end
27
+ end
28
+
29
+ # --------------------------------------------------------------------------
30
+ def render_table
31
+ formatted = data.to_h { |key, entry| [key, format_table_entry(entry, key)] }
32
+
33
+ # Pre-format Entry
34
+ table = TTY::Table.new(header: formatted.keys, rows: [formatted], orientation: :vertical)
35
+
36
+ LogBot.debug('Rendering Entries') if ENV['DEBUG']
37
+ self.output = table.render(table_style, padding: [0, 1, 0, 1], multiline: true) do |renderer|
38
+ renderer.border.style = :cyan
39
+ end
40
+
41
+ # Line breaks for basic tables
42
+ self.output += "\n" if table_style == :basic
43
+ rescue StandardError => e
44
+ if ENV['DEBUG']
45
+ LogBot.warn('Table', message: e.message)
46
+ ap e.backtrace
47
+ end
48
+
49
+ self.output = [
50
+ data.ai,
51
+ ('_' * (TTY::Screen.width / 3)).pastel(:cyan),
52
+ "\n"
53
+ ].join("\n")
54
+ end
55
+
56
+ # --------------------------------------------------------------------------
57
+ # Format Table Entries
58
+ def format_table_entry(entry, key = nil)
59
+ formatted_entry = case entry
60
+ # Rounding
61
+ when Float, Integer || entry.numeric?
62
+ flags.key?(:round) ? entry.to_f.round(flags.round).ai : entry.ai
63
+
64
+ # General Inspecting
65
+ when Hash then entry.ai(ruby19_syntax: true)
66
+
67
+ # Arrays often contain Hashes. Dangerous Recursive?
68
+ when Array
69
+ entry.map { |x| format_table_entry(x) }.join("\n")
70
+ when Time
71
+ entry.to_s.pastel(:bright_white)
72
+
73
+ # Default String Formatting
74
+ else
75
+ ShellHelper::StringColor.do(key, entry)
76
+ end
77
+
78
+ # Stats truncation handled separately
79
+ if truncate && !stats
80
+ entry_truncate(formatted_entry)
81
+ else
82
+ formatted_entry
83
+ end
84
+ rescue StandardError => e
85
+ if ENV['DEBUG']
86
+ LogBot.warn('Table Format Entry', message: e.message)
87
+ ap e.backtrace
88
+ end
89
+ end
90
+
91
+ # --------------------------------------------------------------------------
92
+ def entry_truncate(entry)
93
+ # Ignore if Truncation Off
94
+ return entry if truncate.zero?
95
+
96
+ # Only truncate large strings
97
+ return entry unless entry.instance_of?(String) && entry.size > truncate
98
+
99
+ # Include '...' to indicate truncation
100
+ "#{entry.to_s[0..truncate]} #{'...'.pastel(:bright_blue)}"
101
+ end
102
+
103
+ # --------------------------------------------------------------------------
104
+ def render_array_table(entry)
105
+ header, rows = entry
106
+ table = TTY::Table.new(header: header, rows: rows)
107
+
108
+ LogBot.debug('Rendering Entries') if ENV['DEBUG']
109
+ self.output = table.render(table_style, padding: [0, 1, 0, 1], multiline: true) do |renderer|
110
+ renderer.border.style = :cyan
111
+ end
112
+
113
+ # Line breaks for basic tables
114
+ self.output += "\n" if table_style == :basic
115
+ end
116
+ # --------------------------------------------------------------------------
117
+ end
118
+ end
@@ -0,0 +1,34 @@
1
+ module GreenHat
2
+ # Output Helper
3
+ class Paper
4
+ include PaperHelpers
5
+ include PaperFlagHelper
6
+ attr_accessor :task, :output, :flags, :data, :done, :mutex
7
+
8
+ def initialize(data:, flags:)
9
+ self.mutex = Mutex.new
10
+ self.data = data
11
+ self.flags = flags
12
+ end
13
+
14
+ def async
15
+ return if done # Don't attempt lock if already done
16
+
17
+ mutex.synchronize do
18
+ return if done # Turn if was waiting on lock
19
+
20
+ process
21
+
22
+ self.output ||= '' # If Empty
23
+
24
+ self.done = true
25
+ end
26
+ end
27
+
28
+ def render
29
+ async unless done
30
+
31
+ output
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,98 @@
1
+ # GreenHat Namespace
2
+ module GreenHat
3
+ # Reports Parent Class
4
+ module Reports
5
+ # Used for within GreenHat Use
6
+ class Builder
7
+ include GreenHat::Reports::Shared
8
+
9
+ attr_accessor :store, :flags, :args
10
+
11
+ def initialize(file:, flags:, args:)
12
+ self.store = []
13
+ self.flags = flags
14
+ self.args = args
15
+ instance_eval File.read(file)
16
+ rescue StandardError => e
17
+ print "#{e.message}\n"
18
+ print e.backtrace[0..4].join("\n").pastel(:red)
19
+ end
20
+
21
+ # Adding Methods
22
+ def add(*params)
23
+ store.push params
24
+ end
25
+
26
+ # ------------------------------------------------------------------------
27
+ # Store Methods
28
+ # ------------------------------------------------------------------------
29
+
30
+ def quiet!
31
+ GreenHat::Cli.quiet!
32
+ end
33
+
34
+ def no_prompt!
35
+ # TODO: Should this do anything?
36
+ end
37
+
38
+ # Default Header
39
+ def header(text, color = :bright_yellow)
40
+ add(:header, text, color)
41
+ end
42
+
43
+ # Archive Header
44
+ def archive_header(color = :blue)
45
+ add(:archive_header, color)
46
+ end
47
+
48
+ # Raw
49
+ def cat(name, show_output = true, &block)
50
+ add(:cat, name, show_output, block)
51
+ end
52
+
53
+ # Parsed
54
+ def info(name, show_output = true, &block)
55
+ add(:info, name, show_output, block)
56
+ end
57
+
58
+ # New Line Helepr
59
+ def br
60
+ add :br
61
+ end
62
+
63
+ def puts(text = '')
64
+ add :puts, text
65
+ end
66
+
67
+ # Helper to Print GitLab Version
68
+ def gitlab_version
69
+ add :gitlab_version
70
+ end
71
+
72
+ # Help
73
+ def help(&block)
74
+ add(:help, block)
75
+ end
76
+
77
+ def show(text = '')
78
+ add :show, text
79
+ end
80
+
81
+ # Log Query
82
+ def query(query, show_output = true, &block)
83
+ add(:query, query, show_output, block)
84
+ end
85
+
86
+ # Log Query / Assume Format
87
+ def query_format(query, show_output = true, &block)
88
+ add(:query_format, query, show_output, block)
89
+ end
90
+
91
+ # FastStats Helper
92
+ def faststats(query, subcmd = '')
93
+ add(:faststats, query, subcmd)
94
+ end
95
+ # ------------------------------------------------------------------------
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,101 @@
1
+ # Disable for executor
2
+ # rubocop:disable Style/MixinUsage
3
+ include ActionView::Helpers::DateHelper
4
+ include ActionView::Helpers::NumberHelper
5
+ include GreenHat::Reports::Shared
6
+ # rubocop:enable Style/MixinUsage
7
+
8
+ def add(*params)
9
+ GreenHat::Reports.add(*params)
10
+ end
11
+
12
+ # ==============================================================================
13
+ # General Report Helpers
14
+ # ==============================================================================
15
+ # Supress output
16
+ def quiet!
17
+ GreenHat::Cli.quiet!
18
+ end
19
+
20
+ # Supress output
21
+ def page(value = true)
22
+ ENV['GREENHAT_PAGE'] = value == true ? 'true' : 'false'
23
+ end
24
+
25
+ # Attempt Autoload raw/json
26
+ def no_prompt!
27
+ Settings.settings[:assume_raw] = true
28
+ end
29
+
30
+ # Default Header
31
+ def header(text, color = :bright_yellow)
32
+ add(:header, text, color)
33
+ end
34
+
35
+ # Archive Header
36
+ def archive_header(color = :blue)
37
+ add(:archive_header, color)
38
+ end
39
+
40
+ # Raw
41
+ def cat(name, show_output = true, &block)
42
+ add(:cat, name, show_output, block)
43
+ end
44
+
45
+ # Parsed
46
+ def info(name, show_output = true, &block)
47
+ add(:info, name, show_output, block)
48
+ end
49
+
50
+ # Log Query
51
+ def query(query, show_output = true, &block)
52
+ add(:query, query, show_output, block)
53
+ end
54
+
55
+ # Log Query / Assume Format
56
+ def query_format(query, show_output = true, &block)
57
+ add(:query_format, query, show_output, block)
58
+ end
59
+
60
+ # FastStats Helper
61
+ def faststats(query, subcmd = '')
62
+ add(:faststats, query, subcmd)
63
+ end
64
+
65
+ # New Line Helper
66
+ def br
67
+ add :br
68
+ end
69
+
70
+ def puts(text = '')
71
+ add :puts, text
72
+ end
73
+
74
+ def show(text = '')
75
+ add :show, text
76
+ end
77
+
78
+ # Help
79
+ def help(&block)
80
+ add(:help, block)
81
+ end
82
+
83
+ # Helper to Print GitLab Version
84
+ def gitlab_version
85
+ add :gitlab_version
86
+ end
87
+
88
+ # =================================================================
89
+ # These are here to allow flags/args to be used outside of blocks
90
+ # =================================================================
91
+ def args
92
+ _file_list, _flags, args = GreenHat::Args.parse(ARGV)
93
+ args
94
+ end
95
+
96
+ def flags
97
+ _file_list, flags, _args = GreenHat::Args.parse(ARGV)
98
+
99
+ flags
100
+ end
101
+ # =================================================================