summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG3
-rw-r--r--lib/puppet/parser/functions.rb21
-rwxr-xr-xtest/language/functions.rb28
3 files changed, 52 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index d35ceeef8..37b076cc1 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,4 +1,7 @@
0.22.2 (grover)
+ Added a file() command to read in files with no interpolation. The first
+ found file has its content returned.
+
puppetd now exits if no cert is present in onetime mode. (#533)
The client configuration cache can be safely removed and the client
diff --git a/lib/puppet/parser/functions.rb b/lib/puppet/parser/functions.rb
index 0b94ca76e..cef694b85 100644
--- a/lib/puppet/parser/functions.rb
+++ b/lib/puppet/parser/functions.rb
@@ -231,6 +231,27 @@ module Functions
add_namespace(val)
end
end
+
+ newfunction(:file, :type => :rvalue,
+ :doc => "Return the contents of a file. Multiple files
+ can be passed, and the first file that exists will be read in.") do |vals|
+ ret = nil
+ vals.each do |file|
+ unless file =~ /^#{File::SEPARATOR}/
+ raise Puppet::ParseError, "Files must be fully qualified"
+ end
+ if FileTest.exists?(file)
+ ret = File.read(file)
+ break
+ end
+ end
+ if ret
+ ret
+ else
+ raise Puppet::ParseError, "Could not find any files from %s" %
+ vals.join(", ")
+ end
+ end
end
end
diff --git a/test/language/functions.rb b/test/language/functions.rb
index e3ab1ca1e..941f56c2c 100755
--- a/test/language/functions.rb
+++ b/test/language/functions.rb
@@ -445,6 +445,34 @@ class TestLangFunctions < Test::Unit::TestCase
"class %s was not evaluated" % c)
end
end
+
+ def test_file
+ interp = mkinterp
+ scope = mkscope(:interp => interp)
+
+ file1 = tempfile
+ file2 = tempfile
+ file3 = tempfile
+
+ File.open(file2, "w") { |f| f.puts "yaytest" }
+
+ val = nil
+ assert_nothing_raised("Failed to call file with one arg") do
+ val = scope.function_file([file2])
+ end
+
+ assert_equal("yaytest\n", val, "file() failed")
+
+ assert_nothing_raised("Failed to call file with two args") do
+ val = scope.function_file([file1, file2])
+ end
+
+ assert_equal("yaytest\n", val, "file() failed")
+
+ assert_raise(Puppet::ParseError, "did not fail when files are missing") do
+ val = scope.function_file([file1, file3])
+ end
+ end
end
# $Id$