Class: Shell
- Inherits:
-
Object
- Object
- Shell
- Defined in:
- lib/core/shell.rb
Class Attribute Summary collapse
-
.tmp_stderr ⇒ Object
readonly
Returns the value of attribute tmp_stderr.
-
.verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Class Method Summary collapse
- .abort(message, exit_status = ExitCode::ERROR_DEFAULT) ⇒ Object
- .cmd(*cmd_to_run, capture_stderr: false) ⇒ Object
- .color(message, color_key) ⇒ Object
-
.confirm(message) ⇒ Object
Prompts the user and returns whether they confirmed.
- .debug(prefix, message, sensitive_data_pattern: nil) ⇒ Object
-
.hide_sensitive_data(message, pattern = nil) ⇒ String
Hide sensitive data based on the passed pattern.
- .info(message) ⇒ Object
- .read_from_tmp_stderr ⇒ Object
- .should_hide_output? ⇒ Boolean
- .trap_interrupt ⇒ Object
- .use_tmp_stderr ⇒ Object
- .verbose_mode(verbose) ⇒ Object
- .warn(message) ⇒ Object
- .warn_deprecated(message) ⇒ Object
- .write_to_tmp_stderr(message) ⇒ Object
Class Attribute Details
.tmp_stderr ⇒ Object (readonly)
Returns the value of attribute tmp_stderr.
5 6 7 |
# File 'lib/core/shell.rb', line 5 def tmp_stderr @tmp_stderr end |
.verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
5 6 7 |
# File 'lib/core/shell.rb', line 5 def verbose @verbose end |
Class Method Details
.abort(message, exit_status = ExitCode::ERROR_DEFAULT) ⇒ Object
48 49 50 51 |
# File 'lib/core/shell.rb', line 48 def self.abort(, exit_status = ExitCode::ERROR_DEFAULT) Kernel.warn(color("ERROR: #{}", :red)) exit(exit_status) end |
.cmd(*cmd_to_run, capture_stderr: false) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/core/shell.rb', line 68 def self.cmd(*cmd_to_run, capture_stderr: false) output, status = capture_stderr ? Open3.capture2e(*cmd_to_run) : Open3.capture2(*cmd_to_run) { output: output, success: status.success? } end |
.color(message, color_key) ⇒ Object
26 27 28 |
# File 'lib/core/shell.rb', line 26 def self.color(, color_key) shell.set_color(, color_key) end |
.confirm(message) ⇒ Object
Prompts the user and returns whether they confirmed. Side-effecting on stdout/stdin, so the method name intentionally lacks ‘?` despite returning a boolean.
32 33 34 |
# File 'lib/core/shell.rb', line 32 def self.confirm() # rubocop:disable Naming/PredicateMethod shell.yes?("#{} (y/N)") end |
.debug(prefix, message, sensitive_data_pattern: nil) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/core/shell.rb', line 57 def self.debug(prefix, , sensitive_data_pattern: nil) return unless verbose = hide_sensitive_data(, sensitive_data_pattern) Kernel.warn("\n[#{color(prefix, :red)}] #{}") end |
.hide_sensitive_data(message, pattern = nil) ⇒ String
Hide sensitive data based on the passed pattern
90 91 92 93 94 |
# File 'lib/core/shell.rb', line 90 def self.hide_sensitive_data(, pattern = nil) return unless pattern.is_a?(Regexp) .gsub(pattern, "XXXXXXX") end |
.info(message) ⇒ Object
36 37 38 |
# File 'lib/core/shell.rb', line 36 def self.info() shell.say() end |
.read_from_tmp_stderr ⇒ Object
21 22 23 24 |
# File 'lib/core/shell.rb', line 21 def self.read_from_tmp_stderr tmp_stderr.rewind tmp_stderr.read.strip end |
.should_hide_output? ⇒ Boolean
64 65 66 |
# File 'lib/core/shell.rb', line 64 def self.should_hide_output? tmp_stderr && !verbose end |
.trap_interrupt ⇒ Object
96 97 98 99 100 101 |
# File 'lib/core/shell.rb', line 96 def self.trap_interrupt trap("SIGINT") do puts exit(ExitCode::INTERRUPT) end end |
.use_tmp_stderr ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/core/shell.rb', line 8 def self.use_tmp_stderr @tmp_stderr = Tempfile.create yield @tmp_stderr.close @tmp_stderr = nil end |
.verbose_mode(verbose) ⇒ Object
53 54 55 |
# File 'lib/core/shell.rb', line 53 def self.verbose_mode(verbose) @verbose = verbose end |
.warn(message) ⇒ Object
40 41 42 |
# File 'lib/core/shell.rb', line 40 def self.warn() Kernel.warn(color("WARNING: #{}", :yellow)) end |
.warn_deprecated(message) ⇒ Object
44 45 46 |
# File 'lib/core/shell.rb', line 44 def self.warn_deprecated() Kernel.warn(color("DEPRECATED: #{}", :yellow)) end |
.write_to_tmp_stderr(message) ⇒ Object
17 18 19 |
# File 'lib/core/shell.rb', line 17 def self.write_to_tmp_stderr() tmp_stderr.write() end |