diff options
author | Morgan Haskel <morgan@puppetlabs.com> | 2014-12-17 11:00:23 -0800 |
---|---|---|
committer | Morgan Haskel <morgan@puppetlabs.com> | 2014-12-17 11:00:23 -0800 |
commit | 841b0df21069091ba8d7a307faf17f94945f68d6 (patch) | |
tree | 56c105b8043d4ec642dd8c03784431b470303c61 /lib/puppet/parser | |
parent | 8726caf996472c9c8822d1f697ff02a51abc54df (diff) | |
parent | ef3d42f7bbdf95b21f46e580de309298cad300ea (diff) |
Merge pull request #368 from rfugina/basename
Basename implementation
Diffstat (limited to 'lib/puppet/parser')
-rw-r--r-- | lib/puppet/parser/functions/basename.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/basename.rb b/lib/puppet/parser/functions/basename.rb new file mode 100644 index 0000000..f7e4438 --- /dev/null +++ b/lib/puppet/parser/functions/basename.rb @@ -0,0 +1,34 @@ +module Puppet::Parser::Functions + newfunction(:basename, :type => :rvalue, :doc => <<-EOS + Strips directory (and optional suffix) from a filename + EOS + ) do |arguments| + + if arguments.size < 1 then + raise(Puppet::ParseError, "basename(): No arguments given") + elsif arguments.size > 2 then + raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})") + else + + unless arguments[0].is_a?(String) + raise(Puppet::ParseError, 'basename(): Requires string as first argument') + end + + if arguments.size == 1 then + rv = File.basename(arguments[0]) + elsif arguments.size == 2 then + + unless arguments[1].is_a?(String) + raise(Puppet::ParseError, 'basename(): Requires string as second argument') + end + + rv = File.basename(arguments[0], arguments[1]) + end + + end + + return rv + end +end + +# vim: set ts=2 sw=2 et : |