Sha256: e6b98ee02115eca32f37ebbc5c8dff4f1db70f9ac4a45c525cf78ce6569cc50e

Contents?: true

Size: 1.95 KB

Versions: 6

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module Gitlab
  module QA
    module Docker
      class Command
        attr_reader :args, :stream_output

        # Shell command
        #
        # @param [<String, Array>] cmd
        # @param [<String, Array>] mask_secrets
        # @param [Boolean] stream_output stream command output to stdout directly instead of logger
        def initialize(cmd = nil, mask_secrets: nil, stream_output: false)
          @args = Array(cmd)
          @mask_secrets = Array(mask_secrets)
          @stream_output = stream_output
        end

        def <<(*args)
          tap { @args.concat(args) }
        end

        def volume(from, to, opt = :z)
          tap { @args.push("--volume #{from}:#{to}:#{opt}") }
        end

        def name(identity)
          tap { @args.push("--name #{identity}") }
        end

        def env(name, value)
          tap { @args.push(%(--env #{name}="#{value}")) }
        end

        def port(mapping)
          tap { @args.push("-p #{mapping}") }
        end

        def to_s
          "docker #{@args.join(' ')}"
        end

        # Returns a masked string form of a Command
        #
        # @example
        #   Command.new('a docker command', mask_secrets: 'command').mask_secrets #=> 'a docker *****'
        #   Command.new('a docker command', mask_secrets: %w[docker command]).mask_secrets #=> 'a ***** *****'
        #
        # @return [String] The masked command string
        def mask_secrets
          @mask_secrets.each_with_object(+to_s) { |secret, s| s.gsub!(secret, '*****') }
        end

        def ==(other)
          to_s == other.to_s
        end

        def execute!(&block)
          Docker::Shellout.new(self).execute!(&block)
        rescue Docker::Shellout::StatusError => e
          e.set_backtrace([])

          raise e
        end

        def self.execute(cmd, mask_secrets: nil, &block)
          new(cmd, mask_secrets: mask_secrets).execute!(&block)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gitlab-qa-8.4.2 lib/gitlab/qa/docker/command.rb
gitlab-qa-8.4.1 lib/gitlab/qa/docker/command.rb
gitlab-qa-8.4.0 lib/gitlab/qa/docker/command.rb
gitlab-qa-8.3.2 lib/gitlab/qa/docker/command.rb
gitlab-qa-8.3.1 lib/gitlab/qa/docker/command.rb
gitlab-qa-8.3.0 lib/gitlab/qa/docker/command.rb