class Dpl::Cmd

Represents a shell command

Public Instance Methods

assert?() click to toggle source

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`.

# File lib/dpl/helper/cmd.rb, line 76
def assert?
  !opts[:assert].is_a?(FalseClass)
end
capture?() click to toggle source

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.

# File lib/dpl/helper/cmd.rb, line 92
def capture?
  !!opts[:capture]
end
cmd(secure = true) click to toggle source

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.

# File lib/dpl/helper/cmd.rb, line 19
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
echo() click to toggle source

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.

# File lib/dpl/helper/cmd.rb, line 32
def echo
  "$ #{cmd(false)}"
end
echo?() click to toggle source

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`.

# File lib/dpl/helper/cmd.rb, line 84
def echo?
  !opts[:echo].is_a?(FalseClass)
end
error() click to toggle source

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.

# File lib/dpl/helper/cmd.rb, line 66
def error
  keys = [opts[:assert], key]
  err = lookup(:err, *keys)
  err ? interpolate(err, opts).strip : 'Failed'
end
info() click to toggle source

Returns the log message to output after the command has succeeded

# File lib/dpl/helper/cmd.rb, line 105
def info
  opts[:info]
end
info?() click to toggle source

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.

# File lib/dpl/helper/cmd.rb, line 100
def info?
  !!opts[:info]
end
msg() click to toggle source

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.

# File lib/dpl/helper/cmd.rb, line 45
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?() click to toggle source
# File lib/dpl/helper/cmd.rb, line 52
def msg?
  !!lookup(:msg, opts[:msg], key.is_a?(Symbol) ? key : nil)
end
python?() click to toggle source

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.

# File lib/dpl/helper/cmd.rb, line 127
def python?
  !!opts[:python]
end
retry() click to toggle source

Returns how often to retry the command

# File lib/dpl/helper/cmd.rb, line 132
def retry
  opts[:retry]
end
silence?() click to toggle source

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.

# File lib/dpl/helper/cmd.rb, line 140
def silence?
  !!opts[:silence]
end
success() click to toggle source

Returns the log message to output after the command has succeeded

# File lib/dpl/helper/cmd.rb, line 118
def success
  opts[:success]
end
success?() click to toggle source

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.

# File lib/dpl/helper/cmd.rb, line 113
def success?
  !!opts[:success]
end

Private Instance Methods

interpolate(str, args, opts = {}) click to toggle source
# File lib/dpl/helper/cmd.rb, line 155
def interpolate(str, args, opts = {})
  provider ? provider.interpolate(str, args, opts) : str
end
lookup(type, *keys) click to toggle source
# File lib/dpl/helper/cmd.rb, line 146
def lookup(type, *keys)
  str = provider.send(type, *keys) if provider
  str || keys.detect { |key| key.is_a?(String) }
end
missing(type, *keys) click to toggle source
# File lib/dpl/helper/cmd.rb, line 151
def missing(type, *keys)
  raise("Could not find #{type}: #{keys.compact.map(&:inspect).join(', ')}")
end
python(cmd) click to toggle source

Activates the Python virtualenv for the given Python version.

# File lib/dpl/helper/cmd.rb, line 164
def python(cmd)
  # "bash -c 'source $HOME/virtualenv/python#{opts[:python]}/bin/activate; #{cmd.gsub(/'/, "'\\\\''")}'"
  "source $HOME/virtualenv/python#{opts[:python]}/bin/activate && #{cmd}"
end
silence(str) click to toggle source
# File lib/dpl/helper/cmd.rb, line 159
def silence(str)
  "#{str} > /dev/null 2>&1"
end