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-09-21 19:56:08 +02:00
|
|
|
newfunction(:parseyaml, :type => :rvalue, :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]
|
2016-04-07 12:47:42 +02:00
|
|
|
# in ruby 1.9.3 Psych::SyntaxError is a RuntimeException
|
|
|
|
# this still needs to catch that and work also on rubies that
|
|
|
|
# do not have Psych available.
|
|
|
|
rescue StandardError, Psych::SyntaxError => e
|
2015-09-21 19:56:08 +02:00
|
|
|
if arguments[1]
|
|
|
|
arguments[1]
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
2015-08-24 21:00:18 +02:00
|
|
|
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 :
|