Class: Dpl::Cmd

Inherits:
Struct
  • Object
show all
Defined in:
lib/dpl/helper/cmd.rb

Overview

Represents a shell command

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



3
4
5
# File 'lib/dpl/helper/cmd.rb', line 3

def key
  @key
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



3
4
5
# File 'lib/dpl/helper/cmd.rb', line 3

def opts
  @opts
end

#providerObject

Returns the value of attribute provider

Returns:

  • (Object)

    the current value of provider



3
4
5
# File 'lib/dpl/helper/cmd.rb', line 3

def provider
  @provider
end

Instance Method Details

#assert?Boolean

Whether or not to assert that the command has exited with 0

Returns ‘true` if the option `assert` was given as `true` or not given at all. Returns `false` if the option `assert` was given as `false`.

Returns:

  • (Boolean)


74
75
76
# File 'lib/dpl/helper/cmd.rb', line 74

def assert?
  !opts[:assert].is_a?(FalseClass)
end

#capture?Boolean

Whether or not to capture the commands stdout and stderr

Returns ‘true` if the option `capture` was given as `true`. Returns `false` if the option `capture` was given as `false` or not given.

Returns:

  • (Boolean)


90
91
92
# File 'lib/dpl/helper/cmd.rb', line 90

def capture?
  !!opts[:capture]
end

#cmd(secure = true) ⇒ Object

Returns the shell command string

If a Symbol was passed as a command then the command will be looked up from the provider class’ ‘cmds` declaration.

The command will be interpolated, and can include secrets. See ‘Dpl::Interpolate` for details on interpolating variables.

If the option ‘silence` was passed then stdout and stderr will be redirected to `/dev/null`.

If the option ‘python` was passed then the virtualenv with the given Python version will be activated before executing the command.



17
18
19
20
21
22
23
# File 'lib/dpl/helper/cmd.rb', line 17

def cmd(secure = true)
  cmd = lookup(:cmd, key) || missing(:cmd, key)
  cmd = interpolate(cmd, opts, secure: secure).strip
  cmd = silence(cmd) if silence?
  cmd = python(cmd) if python?
  cmd
end

#echoObject

Returns a log message version of the command string

This is the same as #cmd, except that included secrets will be obfuscated, and a prompt (dollar and space) will be prepended. See ‘Dpl::Interpolate` for details on interpolating variables.



30
31
32
# File 'lib/dpl/helper/cmd.rb', line 30

def echo
  "$ #{cmd(false)}"
end

#echo?Boolean

Whether or not to announce the command with an info level log message

Returns ‘true` if the option `assert` was given as `true` or not given at all. Returns `false` if the option `assert` was given as `false`.

Returns:

  • (Boolean)


82
83
84
# File 'lib/dpl/helper/cmd.rb', line 82

def echo?
  !opts[:echo].is_a?(FalseClass)
end

#errorObject

Returns the log message for a failed command

The message string will be interpolated, but included secrets will be obfuscated. See ‘Dpl::Interpolate` for details on interpolating variables.

If the option ‘assert` was given as a String then it will be used. If the option `assert` was given as a Symbol then it will be looked up from the provider class’ ‘errs` declaration. If the command was given as a Symbol, and it can be found in `errs` then this String will be used.



64
65
66
67
68
# File 'lib/dpl/helper/cmd.rb', line 64

def error
  keys = [opts[:assert], key]
  err = lookup(:err, *keys)
  err ? interpolate(err, opts).strip : 'Failed'
end

#infoObject

Returns the log message to output after the command has succeeded



103
104
105
# File 'lib/dpl/helper/cmd.rb', line 103

def info
  opts[:info]
end

#info?Boolean

Whether or not to output a log message before the command is run

Returns ‘true` if the option `info` was given. Returns `false` if the option `info` was given as `false` or not given.

Returns:

  • (Boolean)


98
99
100
# File 'lib/dpl/helper/cmd.rb', line 98

def info?
  !!opts[:info]
end

#msgObject

Returns the log message for announcing a command

If the option ‘msg` was given as a String then it will be used. If the option `msg` was given as a Symbol then it will be looked up from the provider class’ ‘msgs` declaration.

The message string will be interpolated, but included secrets will be obfuscated. See ‘Dpl::Interpolate` for details on interpolating variables.



43
44
45
46
47
48
# File 'lib/dpl/helper/cmd.rb', line 43

def msg
  msg = lookup(:msg, opts[:msg], key.is_a?(Symbol) ? key : nil)
  msg || missing(:msg, opts[:msg], key)
  msg = interpolate(msg, opts, secure: false).strip
  msg
end

#msg?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/dpl/helper/cmd.rb', line 50

def msg?
  !!lookup(:msg, opts[:msg], key.is_a?(Symbol) ? key : nil)
end

#python?Boolean

Whether or not to activate a Python virtualenv before executing the command

Returns ‘true` if the option `python` was given. Returns `false` if the option `python` was given as `false` or not given.

Returns:

  • (Boolean)


125
126
127
# File 'lib/dpl/helper/cmd.rb', line 125

def python?
  !!opts[:python]
end

#retryObject

Returns how often to retry the command



130
131
132
# File 'lib/dpl/helper/cmd.rb', line 130

def retry
  opts[:retry]
end

#silence?Boolean

Whether or not to redirect the command’s stdout and stderr to ‘/dev/null`

Returns ‘true` if the option `silence` was given. Returns `false` if the option `silence` was given as `false` or not given.

Returns:

  • (Boolean)


138
139
140
# File 'lib/dpl/helper/cmd.rb', line 138

def silence?
  !!opts[:silence]
end

#successObject

Returns the log message to output after the command has succeeded



116
117
118
# File 'lib/dpl/helper/cmd.rb', line 116

def success
  opts[:success]
end

#success?Boolean

Whether or not to output a log message after the command has succeeded

Returns ‘true` if the option `success` was given. Returns `false` if the option `success` was given as `false` or not given.

Returns:

  • (Boolean)


111
112
113
# File 'lib/dpl/helper/cmd.rb', line 111

def success?
  !!opts[:success]
end