Class: Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/core/shell.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.tmp_stderrObject (readonly)

Returns the value of attribute tmp_stderr.



5
6
7
# File 'lib/core/shell.rb', line 5

def tmp_stderr
  @tmp_stderr
end

.verboseObject (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(message, exit_status = ExitCode::ERROR_DEFAULT)
  Kernel.warn(color("ERROR: #{message}", :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(message, color_key)
  shell.set_color(message, color_key)
end

.confirm(message) ⇒ Object



34
35
36
# File 'lib/core/shell.rb', line 34

def self.confirm(message)
  shell.yes?("#{message} (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, message, sensitive_data_pattern: nil)
  return unless verbose

  filtered_message = hide_sensitive_data(message, sensitive_data_pattern)
  Kernel.warn("\n[#{color(prefix, :red)}] #{filtered_message}")
end

.hide_sensitive_data(message, pattern = nil) ⇒ String

Hide sensitive data based on the passed pattern

Examples:

hide_sensitive_data("--token abcd", /(?<=--token )(\S+)/)

Parameters:

  • message (String)

    The message to get processed.

  • pattern (Regexp, nil) (defaults to: nil)

    The regular expression to be used. If not provided, no filter gets applied.

Returns:

  • (String)

    Filtered message.



88
89
90
91
92
# File 'lib/core/shell.rb', line 88

def self.hide_sensitive_data(message, pattern = nil)
  return message unless pattern.is_a?(Regexp)

  message.gsub(pattern, "XXXXXXX")
end

.read_from_tmp_stderrObject



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

.shellObject



8
9
10
# File 'lib/core/shell.rb', line 8

def self.shell
  @shell ||= Thor::Shell::Color.new
end

.should_hide_output?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/core/shell.rb', line 62

def self.should_hide_output?
  tmp_stderr && !verbose
end

.trap_interruptObject



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_stderrObject



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(message)
  Kernel.warn(color("WARNING: #{message}", :yellow))
end

.warn_deprecated(message) ⇒ Object



42
43
44
# File 'lib/core/shell.rb', line 42

def self.warn_deprecated(message)
  Kernel.warn(color("DEPRECATED: #{message}", :yellow))
end

.write_to_tmp_stderr(message) ⇒ Object



21
22
23
# File 'lib/core/shell.rb', line 21

def self.write_to_tmp_stderr(message)
  tmp_stderr.write(message)
end