Class: Inspec::Resources::OneGetPackage

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

Instance Method Summary collapse

Constructor Details

#initialize(package_name) ⇒ OneGetPackage

Returns a new instance of OneGetPackage.



22
23
24
25
26
27
# File 'lib/inspec/resources/oneget.rb', line 22

def initialize(package_name)
  @package_name = package_name

  # verify that this resource is only supported on Windows
  return skip_resource "The `oneget` resource is not supported on your OS." unless inspec.os.windows?
end

Instance Method Details

#infoObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/inspec/resources/oneget.rb', line 29

def info
  return @info if defined?(@info)

  @info = {}
  @info[:type] = "oneget"
  @info[:installed] = false

  cmd = inspec.command("Get-Package -Name '#{@package_name}' | ConvertTo-Json")
  # cannot rely on exit code for now, successful command returns exit code 1
  # return nil if cmd.exit_status != 0
  # try to parse json

  begin
    pkgs = JSON.parse(cmd.stdout)
    @info[:installed] = true

    # sometimes we get multiple values
    if pkgs.is_a?(Array)
      # select the first entry
      pkgs = pkgs.first
    end
  rescue JSON::ParserError => _e
    return @info
  end

  @info[:name] = pkgs["Name"] if pkgs.key?("Name")
  @info[:version] = pkgs["Version"] if pkgs.key?("Version")
  @info
end

#installed?Boolean

Returns:

  • (Boolean)


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

def installed?
  info[:installed] == true
end

#resource_idObject



67
68
69
# File 'lib/inspec/resources/oneget.rb', line 67

def resource_id
  @package_name || "oneget"
end

#to_sObject



71
72
73
# File 'lib/inspec/resources/oneget.rb', line 71

def to_s
  "OneGet Package #{@package_name}"
end

#versionObject



63
64
65
# File 'lib/inspec/resources/oneget.rb', line 63

def version
  info[:version]
end