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
- .debug(prefix, message, sensitive_data_pattern: nil) ⇒ Object
-
.hide_sensitive_data(message, pattern = nil) ⇒ String
Hide sensitive data based on the passed pattern.
- .read_from_tmp_stderr ⇒ Object
- .shell ⇒ 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
46 47 48 49 |
# File 'lib/core/shell.rb', line 46 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
66 67 68 69 70 71 72 73 |
# File 'lib/core/shell.rb', line 66 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
30 31 32 |
# File 'lib/core/shell.rb', line 30 def self.color(, color_key) shell.set_color(, color_key) end |
.confirm(message) ⇒ Object
34 35 36 |
# File 'lib/core/shell.rb', line 34 def self.confirm() shell.yes?("#{} (y/N)") end |
.debug(prefix, message, sensitive_data_pattern: nil) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/core/shell.rb', line 55 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
88 89 90 91 92 |
# File 'lib/core/shell.rb', line 88 def self.hide_sensitive_data(, pattern = nil) return unless pattern.is_a?(Regexp) .gsub(pattern, "XXXXXXX") end |
.read_from_tmp_stderr ⇒ Object
25 26 27 28 |
# File 'lib/core/shell.rb', line 25 def self.read_from_tmp_stderr tmp_stderr.rewind tmp_stderr.read.strip end |
.shell ⇒ Object
8 9 10 |
# File 'lib/core/shell.rb', line 8 def self.shell @shell ||= Thor::Shell::Color.new end |
.should_hide_output? ⇒ Boolean
62 63 64 |
# File 'lib/core/shell.rb', line 62 def self.should_hide_output? tmp_stderr && !verbose end |
.trap_interrupt ⇒ Object
94 95 96 97 98 99 |
# File 'lib/core/shell.rb', line 94 def self.trap_interrupt trap("SIGINT") do puts exit(ExitCode::INTERRUPT) end end |
.use_tmp_stderr ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/core/shell.rb', line 12 def self.use_tmp_stderr @tmp_stderr = Tempfile.create yield @tmp_stderr.close @tmp_stderr = nil end |
.verbose_mode(verbose) ⇒ Object
51 52 53 |
# File 'lib/core/shell.rb', line 51 def self.verbose_mode(verbose) @verbose = verbose end |
.warn(message) ⇒ Object
38 39 40 |
# File 'lib/core/shell.rb', line 38 def self.warn() Kernel.warn(color("WARNING: #{}", :yellow)) end |
.warn_deprecated(message) ⇒ Object
42 43 44 |
# File 'lib/core/shell.rb', line 42 def self.warn_deprecated() Kernel.warn(color("DEPRECATED: #{}", :yellow)) end |
.write_to_tmp_stderr(message) ⇒ Object
21 22 23 |
# File 'lib/core/shell.rb', line 21 def self.write_to_tmp_stderr() tmp_stderr.write() end |