File: dir_split.pp

package info (click to toggle)
puppet-module-extlib 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 420 kB
  • sloc: ruby: 1,035; sh: 15; makefile: 10
file content (19 lines) | stat: -rw-r--r-- 922 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# @summary Splits the given directory or directories into individual paths.
#
# Use this function when you need to split a absolute path into multiple absolute paths
# that all descend from the given path.
#
# @param dirs [Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]]] - either an absolute path or a array of absolute paths.
# @return [Array[String]] - an array of absolute paths after being cut into individual paths.
# @example calling the function
#  extlib::dir_split('/opt/puppetlabs') => ['/opt', '/opt/puppetlabs']
function extlib::dir_split(Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]] *$dirs) >> Array[String] {
  [$dirs].flatten.unique.map | Stdlib::Absolutepath $dir | {
    extlib::dir_clean($dir).split('/').reduce([]) |Array $memo, $value  | {
      empty($value) ? {
        true    => $memo,
        default => $memo + "${memo[-1]}/${value}",
      }
    }
  }.flatten.unique
}