Puppet Function: staging_parse

Defined in:
lib/puppet/parser/functions/staging_parse.rb
Function type:
Ruby 3.x API

Overview

staging_parse()Any

Parse filepath to retrieve information about the file.

Returns:

  • (Any)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppet/parser/functions/staging_parse.rb', line 4

newfunction(:staging_parse, type: :rvalue, doc: <<-EOS
Parse filepath to retrieve information about the file.
EOS
           ) do |arguments|
  if arguments.empty? || arguments.size > 3
    raise(Puppet::ParseError, 'staging_parse(): Wrong number of arguments ' \
      "given (#{arguments.size} for 1, 2, 3)")
  end

  source = arguments[0]
  path   = URI.parse(source.tr('\\', '/')).path

  if path.nil?
    raise Puppet::ParseError, "staging_parse(): #{source.inspect} has no URI " \
                              "'path' component"
  end

  info      = arguments[1] ? arguments[1] : 'filename'
  extension = arguments[2] ? arguments[2] : File.extname(path)

  case info
  when 'filename'
    result = File.basename(path)
  when 'basename'
    result = File.basename(path, extension)
  when 'extname'
    result = File.extname(path)
  when 'parent'
    result = File.expand_path(File.join(path, '..'))
  else
    raise Puppet::ParseError, "staging_parse(), unknown parse info #{info}."
  end

  return result
end