Puppet Function: :'wmflib::dirtree'

Defined in:
modules/wmflib/lib/puppet/functions/wmflib/dirtree.rb
Function type:
Ruby 4.x API

Overview

:'wmflib::dirtree'(String $path)String

Examples:

pars a path

dirtree('/tmp/test/path.conf') => ['/tmp', '/tmp/test']

Parameters:

  • path (String)

    The file path to parse

Returns:

  • (String)

    Returns an array of path elements



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'modules/wmflib/lib/puppet/functions/wmflib/dirtree.rb', line 1

Puppet::Functions.create_function(:'wmflib::dirtree') do
  # returns an array of all parent dirs in a given filepath string
  # @param path The file path to parse
  # @return [String] Returns an array of path elements
  # @example pars a path
  #   dirtree('/tmp/test/path.conf') => ['/tmp', '/tmp/test']
  dispatch :dirtree do
    param 'String', :path
  end
  def dirtree(path)
    raise(Puppet::ParseError, 'dirtree requires a fully qualified path') unless path[0] == '/'
    dirs = []
    tmp_path = path
    loop do
      tmp_path = File.dirname(tmp_path)
      break if tmp_path == '/'
      dirs.push(tmp_path)
    end
    dirs
  end
end