Class: Inspec::Resources::Cmd
- Inherits:
-
Object
- Object
- Inspec::Resources::Cmd
- Defined in:
- lib/resources/command.rb
Direct Known Subclasses
Constant Summary collapse
- SHELLS =
{ 'sh' => ->(x, path = 'sh') { path + ' -c ' + Shellwords.escape(x) }, 'bash' => ->(x, path = 'bash') { path + ' -c ' + Shellwords.escape(x) }, 'zsh' => ->(x, path = 'zsh') { path + ' -c ' + Shellwords.escape(x) }, }.freeze
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
Instance Method Summary collapse
- #exist? ⇒ Boolean
- #exit_status ⇒ Object
-
#initialize(cmd, opts = {}) ⇒ Cmd
constructor
A new instance of Cmd.
- #result ⇒ Object
- #stderr ⇒ Object
- #stdout ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(cmd, opts = {}) ⇒ Cmd
Returns a new instance of Cmd.
30 31 32 33 34 35 36 37 |
# File 'lib/resources/command.rb', line 30 def initialize(cmd, opts = {}) @command = cmd unless opts.is_a?(Hash) skip_resource "Called #{self} with invalid command options. See the resource help for valid examples." opts = {} end @opts = opts end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
28 29 30 |
# File 'lib/resources/command.rb', line 28 def command @command end |
Instance Method Details
#exist? ⇒ Boolean
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/resources/command.rb', line 55 def exist? # silent for mock resources return false if inspec.os[:family].to_s == 'unknown' if inspec.os.linux? res = inspec.backend.run_command("bash -c 'type \"#{@command}\"'") elsif inspec.os.windows? res = inspec.backend.run_command("where.exe \"#{@command}\"") elsif inspec.os.unix? res = inspec.backend.run_command("type \"#{@command}\"") else warn "`command(#{@command}).exist?` is not suported on your OS: #{inspec.os[:family]}" return false end res.exit_status.to_i == 0 end |
#exit_status ⇒ Object
51 52 53 |
# File 'lib/resources/command.rb', line 51 def exit_status result.exit_status.to_i end |
#result ⇒ Object
39 40 41 |
# File 'lib/resources/command.rb', line 39 def result @result ||= inspec.backend.run_command(wrap_cmd) end |
#stderr ⇒ Object
47 48 49 |
# File 'lib/resources/command.rb', line 47 def stderr result.stderr end |
#stdout ⇒ Object
43 44 45 |
# File 'lib/resources/command.rb', line 43 def stdout result.stdout end |
#to_s ⇒ Object
72 73 74 |
# File 'lib/resources/command.rb', line 72 def to_s "Command #{@command}" end |