Class: Inspec::Resources::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/resources/command.rb

Direct Known Subclasses

PowershellScript

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ Cmd

Returns a new instance of Cmd.



22
23
24
# File 'lib/resources/command.rb', line 22

def initialize(cmd)
  @command = cmd
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



20
21
22
# File 'lib/resources/command.rb', line 20

def command
  @command
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/resources/command.rb', line 42

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_statusObject



38
39
40
# File 'lib/resources/command.rb', line 38

def exit_status
  result.exit_status.to_i
end

#resultObject



26
27
28
# File 'lib/resources/command.rb', line 26

def result
  @result ||= inspec.backend.run_command(@command)
end

#stderrObject



34
35
36
# File 'lib/resources/command.rb', line 34

def stderr
  result.stderr
end

#stdoutObject



30
31
32
# File 'lib/resources/command.rb', line 30

def stdout
  result.stdout
end

#to_sObject



59
60
61
# File 'lib/resources/command.rb', line 59

def to_s
  "Command #{@command}"
end