File: module_directory.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (43 lines) | stat: -rw-r--r-- 1,284 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

# Finds an existing module and returns the path to its root directory.
#
# The argument to this function should be a module name String
# For example, the reference `mysql` will search for the
# directory `<MODULES DIRECTORY>/mysql` and return the first
# found on the modulepath.
#
# This function can also accept:
#
# * Multiple String arguments, which will return the path of the **first** module
#  found, skipping non existing modules.
# * An array of module names, which will return the path of the **first** module
#  found from the given names in the array, skipping non existing modules.
#
# The function returns `undef` if none of the given modules were found
#
# @since 5.4.0
#
Puppet::Functions.create_function(:module_directory, Puppet::Functions::InternalFunction) do
  dispatch :module_directory do
    scope_param
    repeated_param 'String', :names
  end

  dispatch :module_directory_array do
    scope_param
    repeated_param 'Array[String]', :names
  end

  def module_directory_array(scope, names)
    module_directory(scope, *names)
  end

  def module_directory(scope, *names)
    names.each do |module_name|
      found = scope.compiler.environment.module(module_name)
      return found.path if found
    end
    nil
  end
end