Sha256: d99e7c9949f26d36f17cee73488050ee401e27e396d75f00bf1eba059f5fefcb

Contents?: true

Size: 1.16 KB

Versions: 19

Compression:

Stored size: 1.16 KB

Contents

require 'json'

# A function that lets you know whether a specific module is on your modulepath.
Puppet::Functions.create_function(:'extlib::has_module') do
  # @param module_name The full name of the module you want to know exists or not.
  #   Namespace and modulename can be separated with either `-` or `/`.
  # @return Returns `true` or `false`.
  # @example Calling the function
  #   extlib::has_module('camptocamp/systemd')
  dispatch :has_module do
    param 'Pattern[/\A\w+[-\/]\w+\z/]', :module_name
    return_type 'Boolean'
  end

  def has_module(module_name) # rubocop:disable Style/PredicateName
    full_module_name = module_name.gsub(%r{/}, '-')
    module_name = full_module_name[%r{(?<=-).+}]
    begin
      module_path = call_function('get_module_path', module_name)
    rescue Puppet::ParseError
      # stdlib function get_module_path raises Puppet::ParseError if module isn't in your environment
      return false
    end

    metadata_json = File.join(module_path, 'metadata.json')

    return false unless File.exist?(metadata_json)

    metadata = JSON.parse(File.read(metadata_json))
    return true if metadata['name'] == full_module_name
    false
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
puppet-debugger-1.4.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-1.3.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-1.2.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-1.1.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-1.0.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.19.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.18.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.17.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.16.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.15.2 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.15.1 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.15.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.14.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.13.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.12.3 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.12.2 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.12.1 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.12.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb
puppet-debugger-0.11.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb