Class: Dpl::Cmd
- Inherits:
-
Struct
- Object
- Struct
- Dpl::Cmd
- Defined in:
- lib/dpl/helper/cmd.rb
Overview
Represents a shell command
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#provider ⇒ Object
Returns the value of attribute provider.
Instance Method Summary collapse
-
#assert? ⇒ Boolean
Whether or not to assert that the command has exited with 0.
-
#capture? ⇒ Boolean
Whether or not to capture the commands stdout and stderr.
-
#cmd(secure = true) ⇒ Object
Returns the shell command string.
-
#echo ⇒ Object
Returns a log message version of the command string.
-
#echo? ⇒ Boolean
Whether or not to announce the command with an info level log message.
-
#error ⇒ Object
Returns the log message for a failed command.
-
#info ⇒ Object
Returns the log message to output after the command has succeeded.
-
#info? ⇒ Boolean
Whether or not to output a log message before the command is run.
-
#msg ⇒ Object
Returns the log message for announcing a command.
- #msg? ⇒ Boolean
-
#python? ⇒ Boolean
Whether or not to activate a Python virtualenv before executing the command.
-
#retry ⇒ Object
Returns how often to retry the command.
-
#silence? ⇒ Boolean
Whether or not to redirect the command’s stdout and stderr to
/dev/null. -
#success ⇒ Object
Returns the log message to output after the command has succeeded.
-
#success? ⇒ Boolean
Whether or not to output a log message after the command has succeeded.
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key
5 6 7 |
# File 'lib/dpl/helper/cmd.rb', line 5 def key @key end |
#opts ⇒ Object
Returns the value of attribute opts
5 6 7 |
# File 'lib/dpl/helper/cmd.rb', line 5 def opts @opts end |
#provider ⇒ Object
Returns the value of attribute provider
5 6 7 |
# File 'lib/dpl/helper/cmd.rb', line 5 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.
76 77 78 |
# File 'lib/dpl/helper/cmd.rb', line 76 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.
92 93 94 |
# File 'lib/dpl/helper/cmd.rb', line 92 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.
19 20 21 22 23 24 25 |
# 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 ⇒ Object
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.
32 33 34 |
# File 'lib/dpl/helper/cmd.rb', line 32 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.
84 85 86 |
# File 'lib/dpl/helper/cmd.rb', line 84 def echo? !opts[:echo].is_a?(FalseClass) end |
#error ⇒ Object
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.
66 67 68 69 70 |
# 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 ⇒ Object
Returns the log message to output after the command has succeeded
105 106 107 |
# File 'lib/dpl/helper/cmd.rb', line 105 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.
100 101 102 |
# File 'lib/dpl/helper/cmd.rb', line 100 def info? !!opts[:info] end |
#msg ⇒ Object
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.
45 46 47 48 49 50 |
# 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? ⇒ Boolean
52 53 54 |
# File 'lib/dpl/helper/cmd.rb', line 52 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.
127 128 129 |
# File 'lib/dpl/helper/cmd.rb', line 127 def python? !!opts[:python] end |
#retry ⇒ Object
Returns how often to retry the command
132 133 134 |
# File 'lib/dpl/helper/cmd.rb', line 132 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.
140 141 142 |
# File 'lib/dpl/helper/cmd.rb', line 140 def silence? !!opts[:silence] end |
#success ⇒ Object
Returns the log message to output after the command has succeeded
118 119 120 |
# File 'lib/dpl/helper/cmd.rb', line 118 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.
113 114 115 |
# File 'lib/dpl/helper/cmd.rb', line 113 def success? !!opts[:success] end |