Class: Inspec::Resources::CpanPackage

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

Instance Method Summary collapse

Constructor Details

#initialize(package_name, perl_lib_path = nil) ⇒ CpanPackage

Returns a new instance of CpanPackage.



20
21
22
23
24
25
26
27
28
# File 'lib/inspec/resources/cpan.rb', line 20

def initialize(package_name, perl_lib_path = nil)
  @package_name = package_name
  @perl_lib_path = perl_lib_path
  @perl_cmd = "perl"

  # this resource is not supported on Windows
  return skip_resource "The `cpan` resource is not supported on your OS yet." if inspec.os.windows?
  return skip_resource "perl not found" unless inspec.command(@perl_cmd).exist?
end

Instance Method Details

#infoObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/inspec/resources/cpan.rb', line 30

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

  @info = {}
  @info[:type] = "cpan"
  @info[:name] = @package_name
  # set PERL5LIB environment variable if a custom lib path is given
  lib_path = @perl_lib_path.nil? ? "" : "PERL5LIB=#{@perl_lib_path} "
  cmd = inspec.command("#{lib_path + @perl_cmd} -le 'eval \"require $ARGV[0]\" and print $ARGV[0]->VERSION or exit 1' #{@package_name}")
  @info[:installed] = cmd.exit_status == 0
  return @info unless cmd.exit_status == 0

  @info[:version] = cmd.stdout.strip
  @info
end

#installed?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/inspec/resources/cpan.rb', line 46

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

#resource_idObject



54
55
56
# File 'lib/inspec/resources/cpan.rb', line 54

def resource_id
  @package_name || "cpan"
end

#to_sObject



58
59
60
# File 'lib/inspec/resources/cpan.rb', line 58

def to_s
  "Perl Module #{@package_name}"
end

#versionObject



50
51
52
# File 'lib/inspec/resources/cpan.rb', line 50

def version
  info[:version]
end