2011-04-30 15:36:25 +02:00
|
|
|
#
|
2011-08-05 09:25:03 +02:00
|
|
|
# parseyaml.rb
|
2011-04-30 15:36:25 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
module Puppet::Parser::Functions
|
2015-08-24 21:00:18 +02:00
|
|
|
newfunction(:parseyaml, :type => :rvalue, :arity => -2, :doc => <<-EOS
|
2012-11-09 18:08:24 +01:00
|
|
|
This function accepts YAML as a string and converts it into the correct
|
2011-07-30 00:09:30 +02:00
|
|
|
Puppet structure.
|
2011-06-29 22:21:55 +02:00
|
|
|
|
2015-08-24 21:00:18 +02:00
|
|
|
The optional second argument can be used to pass a default value that will
|
|
|
|
be returned if the parsing of YAML string have failed.
|
|
|
|
EOS
|
|
|
|
) do |arguments|
|
|
|
|
raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless arguments.length >= 1
|
2011-06-29 22:21:55 +02:00
|
|
|
require 'yaml'
|
|
|
|
|
2015-08-24 21:00:18 +02:00
|
|
|
begin
|
|
|
|
YAML::load(arguments[0]) || arguments[1]
|
|
|
|
rescue Exception
|
|
|
|
arguments[1]
|
|
|
|
end
|
2011-06-29 22:21:55 +02:00
|
|
|
|
2011-04-30 15:36:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 et :
|