From 20fcf0e610535681fa06e19a1aac61fabba30731 Mon Sep 17 00:00:00 2001
From: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Date: Thu, 3 Mar 2011 02:37:26 +0000
Subject: Re-factor of the code to make it more Puppet-friendly and update of
 the short documentation in the comment lines.

---
 load_vars.rb | 109 +++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 61 insertions(+), 48 deletions(-)

diff --git a/load_vars.rb b/load_vars.rb
index 9befba9..0375166 100644
--- a/load_vars.rb
+++ b/load_vars.rb
@@ -1,56 +1,69 @@
-# vim: set ts=2 sw=2 et :
-#
-# load_data loads varibles from external yaml file.
-# 
-# EXAMPLE 1:
-# data.yaml:
-# --
-# host1.client.com:
-#   abc: def
-#   foo: bar
-#   test: other
-# host2.client.com:
-#   abc: abc
-#   foo: baz
-#   test: other2
-#
-# load_vars("/etc/puppet/data.yaml", $fqdn)
-# will try to find matching $fqdn key in data.yaml
-# and, if found, will add variables $abc $foo and $test
-#
-#
-# EXAMPLE 2:
-# data-host1.clent.com.yaml
-# abc: def
-#
-# load_vars("/etc/puppet/data-$fqdn.yaml")
-# will add variable $abc
-
-Puppet::Parser::Functions.newfunction :load_vars, :type => :statement do |args|
-  file = args[0]
-  data = {}
-  if args[1]
-    key = args[1]
-  end
+#!/usr/bin/env ruby
+#
+# load_vars.rb
+#
+# This script will allow for loading variables from an external YAML
+# file and expose then for further use inside the Puppet manifest file ...
+#
+# For example:
+#
+# Given following content of the data.yaml file:
+#
+#  ---
+#  host1.example.com:
+#    foo: bar
+#    baz: quux
+#    question: 42
+#  host2.example.com:
+#    abc: def
+#    this: that
+#    darth: vader
+#
+# Then calling load_vars in Puppet manifest file as follows:
+#
+#  load_vars("/etc/puppet/data.yaml", $fqdn)
+#
+# Will result in addition of variables $foo, $baz and $question
+# for matching host name as per the variable $fqdn ...
+#
+# Another example which uses per-host file:
+#
+# Given following content of the file data-host1.example.com.yaml:
+#
+#  ---
+#  foo: bar
+#
+# Then when we call load_vars like this:
+#
+#  load_vars("/etc/puppet/data-$fqdn.yaml")
+#
+# This will result in a variable $foo being added and ready for use.
+#
 
-  if FileTest.exist?(file) # file exists
+module Puppet::Parser::Functions
+  newfunction(:load_vars, :type => :statement) do |args|
+    data = {}
 
-    data = YAML.load_file(file)
-    raise ArgumentError, "Data in %s is not a hash" % file unless data.is_a?(Hash)
-    # data is a hash for sure
+    file = args[0]
+    key  = args[1] if args[1]
 
-    if key
-      # if we have key then use it:
-      if data[key].is_a?(Hash)
-        data = data[key]
-      else
-        data = {}
+    if File.exists?(file)
+
+      begin
+        data = YAML.load_file(file)
+      rescue => error
+        raise(Puppet::ParseError,
+          "Unable to load data from the file `%s': %s" % file, error.to_s)
       end
+
+      raise(Puppet::ParseError,
+        "Data in the file `%s' is not a hash" % file) unless data.is_a?(Hash)
+
+      data = data[key] if key and data[key].is_a?(Hash)
     end
 
-  end
-  # add values from hash:
-  data.each do |param, value|
-    setvar(param, strinterp(value))
+    data.each { |param, value| setvar(param, strinterp(value)) }
   end
 end
+
+# vim: set ts=2 sw=2 et
-- 
cgit v1.2.3


From e566661cc7a11d4916c98f23cc0f63508fcc3aa2 Mon Sep 17 00:00:00 2001
From: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Date: Thu, 3 Mar 2011 02:45:11 +0000
Subject: Fix to a type in the documentation.

---
 load_vars.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/load_vars.rb b/load_vars.rb
index 0375166..0365cf9 100644
--- a/load_vars.rb
+++ b/load_vars.rb
@@ -3,7 +3,7 @@
 # load_vars.rb
 #
 # This script will allow for loading variables from an external YAML
-# file and expose then for further use inside the Puppet manifest file ...
+# file and expose them for further use inside the Puppet manifest file ...
 #
 # For example:
 #
-- 
cgit v1.2.3