X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Floadyaml.rb;fp=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Floadyaml.rb;h=9696362461283d67666fbdecdf46a08f2f40ff01;hb=6963202b4b62c2816655ac9532521b018fdf83bd;hp=10c400501be487cf14221fdab40ee17ddb681307;hpb=a69999e580f8b3abd12446c2d6ad59e517651813;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb b/3rdparty/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb index 10c400501..969636246 100644 --- a/3rdparty/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb +++ b/3rdparty/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb @@ -1,20 +1,34 @@ module Puppet::Parser::Functions + newfunction(:loadyaml, :type => :rvalue, :arity => -2, :doc => <<-'ENDHEREDOC') do |args| +Load a YAML file containing an array, string, or hash, and return the data +in the corresponding native data type. +The second parameter is the default value. It will be returned if the file +was not found or could not be parsed. - newfunction(:loadyaml, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| - Load a YAML file containing an array, string, or hash, and return the data - in the corresponding native data type. +For example: - For example: + $myhash = loadyaml('/etc/puppet/data/myhash.yaml') + $myhash = loadyaml('no-file.yaml', {'default' => 'value'}) + ENDHEREDOC - $myhash = loadyaml('/etc/puppet/data/myhash.yaml') - ENDHEREDOC + raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1 + require 'yaml' - unless args.length == 1 - raise Puppet::ParseError, ("loadyaml(): wrong number of arguments (#{args.length}; must be 1)") + if File.exists?(args[0]) + begin + YAML::load_file(args[0]) || args[1] + rescue Exception => e + if args[1] + args[1] + else + raise e + end + end + else + warning("Can't load '#{args[0]}' File does not exist!") + args[1] end - YAML.load_file(args[0]) - end end