| 1 |
require 'uri' |
| 2 |
|
| 3 |
module Puppet::Parser::Functions |
| 4 |
newfunction(:staging_parse, :type => :rvalue, :doc => <<-EOS |
| 5 |
Parse filepath to retrieve information about the file. |
| 6 |
EOS |
| 7 |
) do |arguments| |
| 8 |
|
| 9 |
raise(Puppet::ParseError, "staging_parse(): Wrong number of arguments " + |
| 10 |
"given (#{arguments.size} for 1, 2, 3)") if arguments.size < 1 || arguments.size > 3 |
| 11 |
|
| 12 |
source = arguments[0] |
| 13 |
path = URI.parse(source.gsub('\\', '/')).path |
| 14 |
|
| 15 |
raise Puppet::ParseError, "staging_parse(): #{source.inspect} has no URI " + |
| 16 |
"'path' component" if path.nil? |
| 17 |
|
| 18 |
info = arguments[1] ? arguments[1] : 'filename' |
| 19 |
extension = arguments[2] ? arguments[2] : File.extname(path) |
| 20 |
|
| 21 |
case info |
| 22 |
when 'filename' |
| 23 |
result = File.basename(path) |
| 24 |
when 'basename' |
| 25 |
result = File.basename(path, extension) |
| 26 |
when 'extname' |
| 27 |
result = File.extname(path) |
| 28 |
when 'parent' |
| 29 |
result = File.expand_path(File.join(path, '..')) |
| 30 |
else |
| 31 |
raise Puppet::ParseError, "staging_parse(), unknown parse info #{info}." |
| 32 |
end |
| 33 |
|
| 34 |
return result |
| 35 |
end |
| 36 |
end |