Class: Gitlab::QA::Support::ShellCommand
- Inherits:
-
Object
- Object
- Gitlab::QA::Support::ShellCommand
- Defined in:
- lib/gitlab/qa/support/shell_command.rb
Constant Summary collapse
- StatusError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#stream_output ⇒ Object
readonly
Returns the value of attribute stream_output.
Instance Method Summary collapse
-
#execute! ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(command = nil, stdin_data: nil, mask_secrets: nil, stream_output: false) ⇒ ShellCommand
constructor
Shell command.
Constructor Details
#initialize(command = nil, stdin_data: nil, mask_secrets: nil, stream_output: false) ⇒ ShellCommand
Shell command
19 20 21 22 23 24 25 26 |
# File 'lib/gitlab/qa/support/shell_command.rb', line 19 def initialize(command = nil, stdin_data: nil, mask_secrets: nil, stream_output: false) @command = command @mask_secrets = Array(mask_secrets) @stream_output = stream_output @output = [] @logger = Runtime::Logger.logger @stdin_data = stdin_data end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
28 29 30 |
# File 'lib/gitlab/qa/support/shell_command.rb', line 28 def command @command end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
28 29 30 |
# File 'lib/gitlab/qa/support/shell_command.rb', line 28 def output @output end |
#stream_output ⇒ Object (readonly)
Returns the value of attribute stream_output.
28 29 30 |
# File 'lib/gitlab/qa/support/shell_command.rb', line 28 def stream_output @stream_output end |
Instance Method Details
#execute! ⇒ Object
rubocop:disable Metrics/AbcSize
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gitlab/qa/support/shell_command.rb', line 30 def execute! # rubocop:disable Metrics/AbcSize raise StatusError, 'Command already executed' if output.any? logger.info("Shell command: `#{mask_secrets(command).cyan}`") Open3.popen2e(command.to_s) do |stdin, out, wait| if @stdin_data stdin.puts(@stdin_data) stdin.close end out.each do |line| output.push(line) if stream_progress print "." elsif stream_output puts line end yield line, wait if block_given? end puts if stream_progress && !output.empty? fail! if wait.value.exited? && wait.value.exitstatus.nonzero? logger.debug("Shell command output:\n#{string_output}") unless stream_output || output.empty? end string_output end |