Last active
August 9, 2016 19:04
-
-
Save hlindberg/9845638 to your computer and use it in GitHub Desktop.
This is a puppet function that expands two paths to an array of all discrete paths from, to. It is somewhat tested.
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
Puppet::Parser::Functions.newfunction( :paths_between, :type => :rvalue, | |
:arity => 2, | |
:doc => "Produces an array with all paths starting with the first given path to the last. | |
The intended use is to create an array that is used as the title of a file resource that ensure that | |
the list of directories exist, are given the wanted mode and ownership." | |
) do |args| | |
func_name = "paths_between():" | |
from = args[0] | |
to = args[1] | |
unless to.is_a?(String) && from.is_a?(String) | |
raise ArgumentException, "#{func_name} The given paths must be strings" | |
end | |
if from.length < 1 | |
raise ArgumentError, "#{func_name} A path cannot be an empty string" | |
end | |
unless to.start_with?(from) && to.length > from.length | |
raise ArgumentException, "#{func_name} The path #{to} must be a subpath of #{from}" | |
end | |
from_array = from.split(/\/|\\/) | |
to_array = to.split(/\/|\\/).slice(from_array.size()..-1) | |
result = [from] | |
to_array.reduce(from) do |memo, part| | |
memo = File.join(memo, part) | |
result << memo | |
memo | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
This looks like a promising solution on how to create every directory in a path that you don't know ahead, such as a variable. Do you know if it works both on Linux and Windows?