Puppet Function: grafana::get_sub_paths

Defined in:
lib/puppet/functions/grafana/get_sub_paths.rb
Function type:
Ruby 4.x API

Overview

grafana::get_sub_paths(String $inputpath)Array

Function get_sub_paths

This function receives an input path as an input parameter, and returns an array of the subpaths in the input, excluding the input path itself. The function will attempt to ignore any extra slashes in the path given.

This function will only work on UNIX paths with forward slashes (/).

Examples: input = ‘/var/lib/grafana/dashboards’ output = [ ‘/var’, ‘/var/lib’, ‘/var/lib/grafana’/ ]

input = ‘/opt’ output = []

input = ‘/first/second/’ output = [ ‘/first’ ]

Parameters:

  • inputpath (String)

Returns:

  • (Array)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet/functions/grafana/get_sub_paths.rb', line 21

Puppet::Functions.create_function(:'grafana::get_sub_paths') do
  dispatch :get_sub_paths do
    param 'String', :inputpath
    return_type 'Array'
  end

  def get_sub_paths(inputpath)
    ip = inputpath.gsub(%r{/+}, '/')
    allsubs = []
    parts = ip.split('/')
    parts.each_with_index do |value, index|
      next if index.zero? || index == (parts.length - 1)

      allsubs << if index == 1
                   "/#{value}"
                 else
                   "#{allsubs[index - 2]}/#{value}"
                 end
    end
    allsubs
  end
end