aboutsummaryrefslogtreecommitdiff
path: root/lib/puppet/parser
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet/parser')
-rw-r--r--lib/puppet/parser/functions/array_include.rb11
-rw-r--r--lib/puppet/parser/functions/gsub.rb17
-rw-r--r--lib/puppet/parser/functions/split.rb23
-rw-r--r--lib/puppet/parser/functions/tfile.rb18
4 files changed, 29 insertions, 40 deletions
diff --git a/lib/puppet/parser/functions/array_include.rb b/lib/puppet/parser/functions/array_include.rb
new file mode 100644
index 0000000..ce4748d
--- /dev/null
+++ b/lib/puppet/parser/functions/array_include.rb
@@ -0,0 +1,11 @@
+Puppet::Parser::Functions::newfunction(
+ :array_include,
+ :type => :rvalue,
+ :doc => "Checks whether an item is included or not
+
+ Example: array_include(['a','b'],'b') -> true
+ Example: array_include(['a','b'],'c') -> false"
+) do |args|
+ raise Puppet::ParseError, 'array_include() needs two arguments' if args.length != 2
+ args[0].include?(args[1])
+end
diff --git a/lib/puppet/parser/functions/gsub.rb b/lib/puppet/parser/functions/gsub.rb
deleted file mode 100644
index e2410ff..0000000
--- a/lib/puppet/parser/functions/gsub.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-module Puppet::Parser::Functions
- # thin wrapper around the ruby gsub function
- # gsub($string, $pattern, $replacement) will replace all occurrences of
- # $pattern in $string with $replacement. $string can be either a singel
- # value or an array. In the latter case, each element of the array will
- # be processed in turn.
- newfunction(:gsub, :type => :rvalue) do |args|
- if args[0].is_a?(Array)
- args[0].collect do |val|
- val.gsub(/#{args[1]}/, args[2])
- end
- else
- args[0].gsub(/#{args[1]}/, args[2])
- end
- end
-end
-
diff --git a/lib/puppet/parser/functions/split.rb b/lib/puppet/parser/functions/split.rb
deleted file mode 100644
index bffecc1..0000000
--- a/lib/puppet/parser/functions/split.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-# This function has two modes of operation:
-#
-# split($string, $delimiter) : $string
-#
-# Split the first argument on every $delimiter. $delimiter is interpreted as
-# Ruby regular expression.
-#
-# split($string[], $delimiter) : $string[][]
-#
-# Returns an array of split results with the result of applying split to each
-# item from the first argument.
-#
-# For long-term portability it is recommended to refrain from using Ruby's
-# extended RE features.
-module Puppet::Parser::Functions
- newfunction(:split, :type => :rvalue) do |args|
- if args[0].is_a?(Array)
- args.collect do |a| a.split(/#{args[1]}/) end
- else
- args[0].split(/#{args[1]}/)
- end
- end
-end
diff --git a/lib/puppet/parser/functions/tfile.rb b/lib/puppet/parser/functions/tfile.rb
new file mode 100644
index 0000000..a984892
--- /dev/null
+++ b/lib/puppet/parser/functions/tfile.rb
@@ -0,0 +1,18 @@
+Puppet::Parser::Functions::newfunction(
+ :tfile,
+ :type => :rvalue,
+ :doc => "Returns the content of a file. If the file or the path does not
+ yet exist, it will create the path and touch the file."
+) do |args|
+ raise Puppet::ParseError, 'tfile() needs one argument' if args.length != 1
+ path = args.to_a.first
+ unless File.exists?(path)
+ dir = File.dirname(path)
+ unless File.directory?(dir)
+ Puppet::Util.recmkdir(dir,0700)
+ end
+ require 'fileutils'
+ FileUtils.touch(path)
+ end
+ File.read(path)
+end