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



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

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.



5
6
7
# File 'lib/gitlab/qa/docker/command.rb', line 5

def args
  @args
end

#stream_outputObject (readonly)

Returns the value of attribute stream_output.



5
6
7
# File 'lib/gitlab/qa/docker/command.rb', line 5

def stream_output
  @stream_output
end

Class Method Details

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



57
58
59
# File 'lib/gitlab/qa/docker/command.rb', line 57

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

Instance Method Details

#<<(*args) ⇒ Object



18
19
20
# File 'lib/gitlab/qa/docker/command.rb', line 18

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

#==(other) ⇒ Object



49
50
51
# File 'lib/gitlab/qa/docker/command.rb', line 49

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

#env(name, value) ⇒ Object



30
31
32
# File 'lib/gitlab/qa/docker/command.rb', line 30

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

#execute!(&block) ⇒ Object



53
54
55
# File 'lib/gitlab/qa/docker/command.rb', line 53

def execute!(&block)
  Docker::Shellout.new(self).execute!(&block)
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



45
46
47
# File 'lib/gitlab/qa/docker/command.rb', line 45

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

#name(identity) ⇒ Object



26
27
28
# File 'lib/gitlab/qa/docker/command.rb', line 26

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

#to_sObject



34
35
36
# File 'lib/gitlab/qa/docker/command.rb', line 34

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

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



22
23
24
# File 'lib/gitlab/qa/docker/command.rb', line 22

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