Class: FileSystem
- Inherits:
-
Object
- Object
- FileSystem
- Defined in:
- lib/jirametrics/file_system.rb
Overview
This is the stderr display/logging layer: it writes progress and log output straight to $stderr (see the $stderr.print/.flush calls). That output isn't warnings, so $stderr.puts is intentional throughout rather than warn. rubocop:disable Style/StderrPuts
Instance Attribute Summary collapse
-
#log_only ⇒ Object
Returns the value of attribute log_only.
-
#logfile ⇒ Object
Returns the value of attribute logfile.
-
#logfile_name ⇒ Object
Returns the value of attribute logfile_name.
Instance Method Summary collapse
-
#compress(node) ⇒ Object
In some Jira instances, a sizeable portion of the JSON is made up of empty fields.
- #deprecated(message:, date:, depth: 2) ⇒ Object
- #diagnostic(message, more: nil) ⇒ Object
- #dir_exist?(path) ⇒ Boolean
- #end_progress ⇒ Object
- #error(message, more: nil) ⇒ Object
- #file_exist?(filename) ⇒ Boolean
- #foreach(root, &block) ⇒ Object
-
#initialize ⇒ FileSystem
constructor
A new instance of FileSystem.
-
#load(filename) ⇒ Object
Effectively the same as File.read except it forces the encoding to UTF-8.
- #load_json(filename, fail_on_error: true) ⇒ Object
- #log(message, more: nil, also_write_to_stderr: false) ⇒ Object
- #log_start(message) ⇒ Object
- #mkdir(path) ⇒ Object
- #progress_dot ⇒ Object
-
#prunable?(value) ⇒ Boolean
A value worth dropping from the compressed output: nothing there, or an empty array.
- #save_file(content:, filename:) ⇒ Object
- #save_json(json:, filename:) ⇒ Object
- #start_progress ⇒ Object
- #unlink(filename) ⇒ Object
- #utime(file:, time:) ⇒ Object
- #warning(message, more: nil) ⇒ Object
Constructor Details
#initialize ⇒ FileSystem
Returns a new instance of FileSystem.
12 13 14 15 16 17 18 |
# File 'lib/jirametrics/file_system.rb', line 12 def initialize # In almost all cases, this will be immediately replaced in the Exporter # but if we fail before we get that far, this will at least let a useful # error show up on the console. @logfile = $stdout @log_only = false end |
Instance Attribute Details
#log_only ⇒ Object
Returns the value of attribute log_only.
10 11 12 |
# File 'lib/jirametrics/file_system.rb', line 10 def log_only @log_only end |
#logfile ⇒ Object
Returns the value of attribute logfile.
10 11 12 |
# File 'lib/jirametrics/file_system.rb', line 10 def logfile @logfile end |
#logfile_name ⇒ Object
Returns the value of attribute logfile_name.
10 11 12 |
# File 'lib/jirametrics/file_system.rb', line 10 def logfile_name @logfile_name end |
Instance Method Details
#compress(node) ⇒ Object
In some Jira instances, a sizeable portion of the JSON is made up of empty fields. I've seen cases where this simple compression will drop the filesize by half.
106 107 108 109 110 111 112 113 114 |
# File 'lib/jirametrics/file_system.rb', line 106 def compress node if node.is_a? Hash node.reject! { |_key, value| prunable? value } node.each_value { |value| compress value } elsif node.is_a? Array node.each { |element| compress element } end node end |
#deprecated(message:, date:, depth: 2) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/jirametrics/file_system.rb', line 137 def deprecated message:, date:, depth: 2 text = +'' text << "Deprecated(#{date}): " text << caller(1..depth).each do |line| text << "\n-> Called from #{line}" end log text, also_write_to_stderr: true end |
#diagnostic(message, more: nil) ⇒ Object
58 59 60 |
# File 'lib/jirametrics/file_system.rb', line 58 def diagnostic , more: nil log " [diag] #{}", more: more end |
#dir_exist?(path) ⇒ Boolean
129 130 131 |
# File 'lib/jirametrics/file_system.rb', line 129 def dir_exist? path File.exist?(path) && File.directory?(path) end |
#end_progress ⇒ Object
98 99 100 101 102 |
# File 'lib/jirametrics/file_system.rb', line 98 def end_progress return if log_only $stderr.puts '' end |
#error(message, more: nil) ⇒ Object
54 55 56 |
# File 'lib/jirametrics/file_system.rb', line 54 def error , more: nil log "Error: #{}", more: more, also_write_to_stderr: true end |
#file_exist?(filename) ⇒ Boolean
125 126 127 |
# File 'lib/jirametrics/file_system.rb', line 125 def file_exist? filename File.exist?(filename) && File.file?(filename) end |
#foreach(root, &block) ⇒ Object
121 122 123 |
# File 'lib/jirametrics/file_system.rb', line 121 def foreach root, &block Dir.foreach root, &block end |
#load(filename) ⇒ Object
Effectively the same as File.read except it forces the encoding to UTF-8
21 22 23 |
# File 'lib/jirametrics/file_system.rb', line 21 def load filename File.read filename, encoding: 'UTF-8' end |
#load_json(filename, fail_on_error: true) ⇒ Object
25 26 27 28 29 |
# File 'lib/jirametrics/file_system.rb', line 25 def load_json filename, fail_on_error: true return nil if fail_on_error == false && File.exist?(filename) == false JSON.parse load(filename) end |
#log(message, more: nil, also_write_to_stderr: false) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/jirametrics/file_system.rb', line 62 def log , more: nil, also_write_to_stderr: false += " See #{logfile_name} for more details about this message." if more logfile.puts logfile.puts more if more return if log_only || !also_write_to_stderr # Obscure edge-case where we're trying to log something before logging is even # set up. Quick escape here so that we don't dump the error twice. return if logfile == $stdout $stderr.puts end |
#log_start(message) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/jirametrics/file_system.rb', line 76 def log_start logfile.puts return if log_only || logfile == $stdout $stderr.print $stderr.flush end |
#mkdir(path) ⇒ Object
42 43 44 |
# File 'lib/jirametrics/file_system.rb', line 42 def mkdir path FileUtils.mkdir_p path end |
#progress_dot ⇒ Object
91 92 93 94 95 96 |
# File 'lib/jirametrics/file_system.rb', line 91 def progress_dot return if log_only $stderr.print '.' $stderr.flush end |
#prunable?(value) ⇒ Boolean
A value worth dropping from the compressed output: nothing there, or an empty array.
117 118 119 |
# File 'lib/jirametrics/file_system.rb', line 117 def prunable? value value.nil? || (value.is_a?(Array) && value.empty?) end |
#save_file(content:, filename:) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/jirametrics/file_system.rb', line 35 def save_file content:, filename: file_path = File.dirname(filename) FileUtils.mkdir_p file_path unless File.exist?(file_path) File.write(filename, content) end |
#save_json(json:, filename:) ⇒ Object
31 32 33 |
# File 'lib/jirametrics/file_system.rb', line 31 def save_json json:, filename: save_file content: JSON.pretty_generate(compress json), filename: filename end |
#start_progress ⇒ Object
84 85 86 87 88 89 |
# File 'lib/jirametrics/file_system.rb', line 84 def start_progress return if log_only $stderr.print ' ' $stderr.flush end |
#unlink(filename) ⇒ Object
133 134 135 |
# File 'lib/jirametrics/file_system.rb', line 133 def unlink filename File.unlink filename end |
#utime(file:, time:) ⇒ Object
46 47 48 |
# File 'lib/jirametrics/file_system.rb', line 46 def utime file:, time: File.utime time, time, file end |
#warning(message, more: nil) ⇒ Object
50 51 52 |
# File 'lib/jirametrics/file_system.rb', line 50 def warning , more: nil log "Warning: #{}", more: more, also_write_to_stderr: true end |