diff options
author | Bryan Jen <bryan.jen@gmail.com> | 2016-05-12 14:14:14 -0700 |
---|---|---|
committer | Bryan Jen <bryan.jen@gmail.com> | 2016-05-12 14:14:14 -0700 |
commit | ecfdbb2690359434f1737d05ce723cf2f5d05c4e (patch) | |
tree | 7c90052d6cd3bb4d4f15a4b347ba6017360f3a42 /lib/puppet | |
parent | 7a008a70b6faa8e06df966677376cb84a80c25e5 (diff) | |
parent | 870a272cee6889934d60c4bfd7a814bcf47011f1 (diff) |
Merge pull request #600 from dmitryilyin/master
Add the default value to the "loadyaml" function
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/parser/functions/loadjson.rb | 34 | ||||
-rw-r--r-- | lib/puppet/parser/functions/loadyaml.rb | 37 |
2 files changed, 57 insertions, 14 deletions
diff --git a/lib/puppet/parser/functions/loadjson.rb b/lib/puppet/parser/functions/loadjson.rb new file mode 100644 index 0000000..3a3372b --- /dev/null +++ b/lib/puppet/parser/functions/loadjson.rb @@ -0,0 +1,34 @@ +module Puppet::Parser::Functions + newfunction(:loadjson, :type => :rvalue, :arity => -2, :doc => <<-'ENDHEREDOC') do |args| +Load a JSON 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. + +For example: + + $myhash = loadjson('/etc/puppet/data/myhash.json') + $myhash = loadjson('no-file.json', {'default' => 'value'}) + ENDHEREDOC + + raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1 + + if File.exists?(args[0]) + begin + content = File.read(args[0]) + PSON::load(content) || 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 + + end + +end diff --git a/lib/puppet/parser/functions/loadyaml.rb b/lib/puppet/parser/functions/loadyaml.rb index ca655f6..9696362 100644 --- a/lib/puppet/parser/functions/loadyaml.rb +++ b/lib/puppet/parser/functions/loadyaml.rb @@ -1,23 +1,32 @@ 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)") - end - - if File.exists?(args[0]) then - YAML.load_file(args[0]) + 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!") - nil + warning("Can't load '#{args[0]}' File does not exist!") + args[1] end end |