Class: Gitlab::QA::Docker::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/qa/docker/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd = nil, mask_secrets: nil, stream_output: false) ⇒ Command

Shell command

Parameters:

  • cmd (<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



14
15
16
17
18
# File 'lib/gitlab/qa/docker/command.rb', line 14

def initialize(cmd = nil, mask_secrets: nil, stream_output: false)
  @args = Array(cmd)
  @mask_secrets = Array(mask_secrets)
  @stream_output = stream_output
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/gitlab/qa/docker/command.rb', line 7

def args
  @args
end

#stream_outputObject (readonly)

Returns the value of attribute stream_output.



7
8
9
# File 'lib/gitlab/qa/docker/command.rb', line 7

def stream_output
  @stream_output
end

Class Method Details

.execute(cmd, mask_secrets: nil, &block) ⇒ Object



67
68
69
# File 'lib/gitlab/qa/docker/command.rb', line 67

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

Instance Method Details

#<<(*args) ⇒ Object



20
21
22
# File 'lib/gitlab/qa/docker/command.rb', line 20

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

#==(other) ⇒ Object



55
56
57
# File 'lib/gitlab/qa/docker/command.rb', line 55

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

#env(name, value) ⇒ Object



32
33
34
# File 'lib/gitlab/qa/docker/command.rb', line 32

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

#execute!(&block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/gitlab/qa/docker/command.rb', line 59

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

  raise e
end

#mask_secretsString

Returns a masked string form of a Command

Examples:

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 ***** *****'

Returns:

  • (String)

    The masked command string



51
52
53
# File 'lib/gitlab/qa/docker/command.rb', line 51

def mask_secrets
  @mask_secrets.each_with_object(+to_s) { |secret, s| s.gsub!(secret, '*****') }
end

#name(identity) ⇒ Object



28
29
30
# File 'lib/gitlab/qa/docker/command.rb', line 28

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

#port(mapping) ⇒ Object



36
37
38
# File 'lib/gitlab/qa/docker/command.rb', line 36

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

#to_sObject



40
41
42
# File 'lib/gitlab/qa/docker/command.rb', line 40

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

#volume(from, to, opt = :z) ⇒ Object



24
25
26
# File 'lib/gitlab/qa/docker/command.rb', line 24

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