Sha256: 5416d2976ea7bbba66f4035a83f9363620ba13c42e5b01d425bf6c4bbf7ea19a

Contents?: true

Size: 1.88 KB

Versions: 18

Compression:

Stored size: 1.88 KB

Contents

# encoding: utf-8
# author: Christoph Hartmann
# author: Dominik Richter

# This resource talks with OneGet (https://github.com/OneGet/oneget)
# Its part of Windows Management Framework 5.0 and part of Windows 10
#
# Usage:
# describe oneget('zoomit') do
#   it { should be_installed }
# end
class OneGetPackage < Inspec.resource(1)
  name 'oneget'
  desc 'Use the oneget InSpec audit resource to test if the named package and/or package version is installed on the system. This resource uses OneGet, which is part of the Windows Management Framework 5.0 and Windows 10. This resource uses the Get-Package cmdlet to return all of the package names in the OneGet repository.'
  example "
    describe oneget('zoomit') do
      it { should be_installed }
    end
  "

  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.' if inspec.os[:family] != 'windows'
  end

  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

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

  def version
    info[:version]
  end

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
inspec-0.14.8 lib/resources/oneget.rb
inspec-0.14.7 lib/resources/oneget.rb
inspec-0.14.6 lib/resources/oneget.rb
inspec-0.14.5 lib/resources/oneget.rb
inspec-0.14.4 lib/resources/oneget.rb
inspec-0.14.3 lib/resources/oneget.rb
inspec-0.14.2 lib/resources/oneget.rb
inspec-0.14.1 lib/resources/oneget.rb
inspec-0.14.0 lib/resources/oneget.rb
inspec-0.12.0 lib/resources/oneget.rb
inspec-0.11.0 lib/resources/oneget.rb
inspec-0.10.1 lib/resources/oneget.rb
inspec-0.9.11 lib/resources/oneget.rb
inspec-0.9.10 lib/resources/oneget.rb
inspec-0.9.9 lib/resources/oneget.rb
inspec-0.9.8 lib/resources/oneget.rb
inspec-0.9.7 lib/resources/oneget.rb
inspec-0.9.6 lib/resources/oneget.rb