Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / is_absolute_path.rb
1 #
2 # is_absolute_path.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:is_absolute_path, :type => :rvalue, :arity => 1, :doc => <<-'DOC') do |args|
6     Returns boolean true if the string represents an absolute path in the filesystem.  This function works
7     for windows and unix style paths.
8
9     The following values will return true:
10
11         $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
12         is_absolute_path($my_path)
13         $my_path2 = '/var/lib/puppet'
14         is_absolute_path($my_path2)
15         $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet']
16         is_absolute_path($my_path3)
17         $my_path4 = ['/var/lib/puppet']
18         is_absolute_path($my_path4)
19
20     The following values will return false:
21
22         is_absolute_path(true)
23         is_absolute_path('../var/lib/puppet')
24         is_absolute_path('var/lib/puppet')
25         $undefined = undef
26         is_absolute_path($undefined)
27
28   DOC
29     function_deprecation([:is_absolute_path, 'This method is deprecated, please use the stdlib validate_legacy function,
30                            with Stdlib::Compat::Absolute_path. There is further documentation for validate_legacy function in the README.'])
31     require 'puppet/util'
32
33     path = args[0]
34     # This logic was borrowed from
35     # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
36     # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
37     if Puppet::Util.respond_to?(:absolute_path?)
38       value = (Puppet::Util.absolute_path?(path, :posix) || Puppet::Util.absolute_path?(path, :windows))
39     else
40       # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
41       # Determine in a platform-specific way whether a path is absolute. This
42       # defaults to the local platform if none is specified.
43       # Escape once for the string literal, and once for the regex.
44       slash = '[\\\\/]'
45       name = '[^\\\\/]+'
46       regexes = {
47         :windows => %r{^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))}i,
48         :posix => %r{^/},
49       }
50       value = !!(path =~ regexes[:posix]) || !!(path =~ regexes[:windows]) # rubocop:disable Style/DoubleNegation : No alternative known
51     end
52     value
53   end
54 end