Puppet Function: plugin_dir

Defined in:
lib/puppet/parser/functions/plugin_dir.rb
Function type:
Ruby 3.x API

Overview

plugin_dir()Any

Extracts the end plugin directory of the name

Returns:

  • (Any)

    String



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
44
# File 'lib/puppet/parser/functions/plugin_dir.rb', line 2

newfunction(:plugin_dir, :type => :rvalue, :doc => <<-EOS
  Extracts the end plugin directory of the name

  @return String
  EOS
) do |arguments|

  if arguments.size < 1 then
    raise(Puppet::ParseError, "plugin_dir(): No arguments given")
  elsif arguments.size > 2 then
    raise(Puppet::ParseError, "plugin_dir(): Too many arguments given (#{arguments.size})")
  else

    unless arguments[0].is_a?(String)
      raise(Puppet::ParseError, 'plugin_dir(): Requires string as first argument')
    end

    plugin_name = arguments[0]
    items = plugin_name.split("/")

    if items.count == 1
      endname = items[0]
    elsif items.count > 1
      plugin = items[1]
      if plugin.include?('-') # example elasticsearch-head
        if plugin.start_with?('elasticsearch-')
          endname = plugin.gsub('elasticsearch-', '')
        elsif plugin.start_with?('es-')
          endname = plugin.gsub('es-', '')
        else
          endname = plugin
        end
      else
        endname = plugin
      end
    else
      raise(Puppet::ParseError, "Unable to parse plugin name: #{plugin_name}")
    end

    return endname

  end
end