lib/core/shell.rb in cpl-1.4.0 vs lib/core/shell.rb in cpl-2.2.0
- old
+ new
@@ -7,14 +7,10 @@
def self.shell
@shell ||= Thor::Shell::Color.new
end
- def self.stderr
- @stderr ||= $stderr
- end
-
def self.use_tmp_stderr
@tmp_stderr = Tempfile.create
yield
@@ -38,36 +34,46 @@
def self.confirm(message)
shell.yes?("#{message} (y/N)")
end
def self.warn(message)
- stderr.puts(color("WARNING: #{message}", :yellow))
+ Kernel.warn(color("WARNING: #{message}", :yellow))
end
def self.warn_deprecated(message)
- stderr.puts(color("DEPRECATED: #{message}", :yellow))
+ Kernel.warn(color("DEPRECATED: #{message}", :yellow))
end
- def self.abort(message)
- Kernel.abort(color("ERROR: #{message}", :red))
+ def self.abort(message, exit_status = ExitCode::ERROR_DEFAULT)
+ Kernel.warn(color("ERROR: #{message}", :red))
+ exit(exit_status)
end
def self.verbose_mode(verbose)
@verbose = verbose
end
def self.debug(prefix, message, sensitive_data_pattern: nil)
return unless verbose
filtered_message = hide_sensitive_data(message, sensitive_data_pattern)
- stderr.puts("\n[#{color(prefix, :red)}] #{filtered_message}")
+ Kernel.warn("\n[#{color(prefix, :red)}] #{filtered_message}")
end
def self.should_hide_output?
tmp_stderr && !verbose
end
+ 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
+
#
# Hide sensitive data based on the passed pattern
#
# @param [String] message
# The message to get processed.
@@ -81,7 +87,14 @@
# hide_sensitive_data("--token abcd", /(?<=--token )(\S+)/)
def self.hide_sensitive_data(message, pattern = nil)
return message unless pattern.is_a?(Regexp)
message.gsub(pattern, "XXXXXXX")
+ end
+
+ def self.trap_interrupt
+ trap("SIGINT") do
+ puts
+ exit(ExitCode::INTERRUPT)
+ end
end
end