diff --git a/functions/dir_split.pp b/functions/dir_split.pp index 47429db..b74ae74 100644 --- a/functions/dir_split.pp +++ b/functions/dir_split.pp @@ -1,25 +1,25 @@ # @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] { $sep = extlib::file_separator() $dirs_array = [$dirs].flatten.unique.map | Stdlib::Absolutepath $dir | { $dir.split(shell_escape($sep)).reduce([]) |Array $acc, $value | { - $counter = $acc.length - 1 - $acc_value = ($acc[$counter] =~ Undef) ? { true => '', false => $acc[$counter] } - unless empty($value) { - $acc + extlib::path_join([$acc_value, $value]) - } else { - $acc - } + $counter = $acc.length - 1 + $acc_value = ($acc[$counter] =~ Undef) ? { true => '', false => $acc[$counter] } + unless empty($value) { + $acc + extlib::path_join([$acc_value, $value]) + } else { + $acc } + } } $dirs_array.flatten.unique } diff --git a/functions/mkdir_p.pp b/functions/mkdir_p.pp index f198e1c..ddefe56 100644 --- a/functions/mkdir_p.pp +++ b/functions/mkdir_p.pp @@ -1,20 +1,20 @@ # @summary Like the unix command mkdir_p except with puppet code. # This creates file resources for all directories and utilizes the dir_split() function # to get a list of all the descendant directories. You will have no control over any other parameters # for the file resource. If you wish to control the file resources you can use the dir_split() function # and get an array of directories for use in your own code. Please note this does not use an exec resource. # # @param dirs [Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]]] - the path(s) to create # @return [Array[Stdlib::Absolutepath]] # @example How to use # extlib::mkdir_p('/opt/puppetlabs/bin') => ['/opt', '/opt/puppetlabs', '/opt/puppetlabs/bin'] # @note splits the given directories into paths that are then created using file resources # @note if you wish to create the directories manually you can use the extlib::dir_split() function in the same manner function extlib::mkdir_p(Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]] $dirs) >> Array[Stdlib::Absolutepath] { $dirs_array = extlib::dir_split($dirs) - @file{$dirs_array: + @file { $dirs_array: ensure => directory, } realize(File[$dirs_array]) $dirs_array }