Class: Gitlab::QA::Support::ShellCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/qa/support/shell_command.rb

Constant Summary collapse

StatusError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command = nil, stdin_data: nil, mask_secrets: nil, stream_output: false) ⇒ ShellCommand

Shell command

Parameters:

  • command (<String, Array>) (defaults to: nil)
  • mask_secrets (<String, Array>) (defaults to: nil)
  • stream_output (Boolean) (defaults to: false)

    stream command output to stdout directly instead of logger



20
21
22
23
24
25
26
27
# File 'lib/gitlab/qa/support/shell_command.rb', line 20

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

#commandObject (readonly)

Returns the value of attribute command.



29
30
31
# File 'lib/gitlab/qa/support/shell_command.rb', line 29

def command
  @command
end

#outputObject (readonly)

Returns the value of attribute output.



29
30
31
# File 'lib/gitlab/qa/support/shell_command.rb', line 29

def output
  @output
end

#stream_outputObject (readonly)

Returns the value of attribute stream_output.



29
30
31
# File 'lib/gitlab/qa/support/shell_command.rb', line 29

def stream_output
  @stream_output
end

Instance Method Details

#execute!Object

rubocop:disable Metrics/AbcSize

Raises:



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
61
# File 'lib/gitlab/qa/support/shell_command.rb', line 31

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