-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
481 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 | ||
} | ||
} | ||
} | ||
$dirs_array.flatten.unique | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# @summary Returns the os specific file path separator. | ||
# @return [String] - The os specific path separator. | ||
# @example Example of how to use | ||
# extlib::file_separator() => '/' | ||
function extlib::file_separator() >> String { | ||
($::facts['kernel'] == 'windows' ) ? { true => "\\", false => '/' } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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: | ||
ensure => directory, | ||
} | ||
realize(File[$dirs_array]) | ||
$dirs_array | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# @summary Take one or more paths and join them together using the os specific separator. | ||
# Because in how windows uses a different separator this function | ||
# will format a windows path into a equilivent unix like path. This type of unix like | ||
# path will work on windows. | ||
# | ||
# @param dirs [Array[String]] - Joins two or more directories by file separator. | ||
# @return [Stdlib::Absolutepath] - The joined path | ||
# @example extlib::path_join('/tmp', 'test', 'libs') returns '/tmp/test/libs' | ||
# @example extlib::path_join('c:', 'test', 'libs') returns '/c/test/libs' | ||
function extlib::path_join(Array[String] $dirs) >> Stdlib::Absolutepath { | ||
$unix_sep = '/' | ||
$sep_regex = /\/|\\/ | ||
$first_value = $dirs[0] | ||
# when first value is absolute path, append all other elements | ||
# by breaking the path into pieces first, then joining | ||
if $first_value =~ Stdlib::Absolutepath { | ||
$fixed_dirs = $first_value.split($sep_regex) + $dirs.delete($first_value) | ||
} else { | ||
$fixed_dirs = $dirs | ||
} | ||
$no_empty_dirs = $fixed_dirs.filter |$dir| { !empty($dir) } | ||
$dirs_without_sep = $no_empty_dirs.map |String $dir | { | ||
# remove : and file separator | ||
$dir.regsubst($sep_regex, '').regsubst(':', '') | ||
} | ||
join([$unix_sep,$dirs_without_sep.join($unix_sep)]) | ||
} |
Oops, something went wrong.