summaryrefslogtreecommitdiff
path: root/lib/puppet/parser
diff options
context:
space:
mode:
authormh <mh@immerda.ch>2009-10-31 15:16:37 +0100
committerMicah Anderson <micah@riseup.net>2010-09-02 18:11:43 -0400
commit97e15ea5a84bf414529e110a4215d9fe7f9f20e0 (patch)
tree68e13250528618b13102cfc618f726e0d3f66b1b /lib/puppet/parser
parent37b37ae82fbc2867ac759e627f53c5e6a8be687a (diff)
move plugin directory to fit new 0.25 style
Diffstat (limited to 'lib/puppet/parser')
-rw-r--r--lib/puppet/parser/functions/basename.rb16
-rw-r--r--lib/puppet/parser/functions/dirname.rb16
-rw-r--r--lib/puppet/parser/functions/gsub.rb17
-rw-r--r--lib/puppet/parser/functions/hostname.rb13
-rw-r--r--lib/puppet/parser/functions/prefix_with.rb9
-rw-r--r--lib/puppet/parser/functions/re_escape.rb7
-rw-r--r--lib/puppet/parser/functions/sha1.rb9
-rw-r--r--lib/puppet/parser/functions/slash_escape.rb7
-rw-r--r--lib/puppet/parser/functions/split.rb17
-rw-r--r--lib/puppet/parser/functions/strlength.rb6
-rw-r--r--lib/puppet/parser/functions/substitute.rb20
11 files changed, 137 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/basename.rb b/lib/puppet/parser/functions/basename.rb
new file mode 100644
index 0000000..226d6e5
--- /dev/null
+++ b/lib/puppet/parser/functions/basename.rb
@@ -0,0 +1,16 @@
+# basename(string) : string
+# basename(string[]) : string[]
+#
+# Returns the last component of the filename given as argument, which must be
+# formed using forward slashes (``/..) regardless of the separator used on the
+# local file system.
+module Puppet::Parser::Functions
+ newfunction(:basename, :type => :rvalue) do |args|
+ if args[0].is_a?(Array)
+ args.collect do |a| File.basename(a) end
+ else
+ File.basename(args[0])
+ end
+ end
+end
+
diff --git a/lib/puppet/parser/functions/dirname.rb b/lib/puppet/parser/functions/dirname.rb
new file mode 100644
index 0000000..44b4a00
--- /dev/null
+++ b/lib/puppet/parser/functions/dirname.rb
@@ -0,0 +1,16 @@
+# dirname(string) : string
+# dirname(string[]) : string[]
+#
+# Returns all components of the filename given as argument except the last
+# one. The filename must be formed using forward slashes (``/..) regardless of
+# the separator used on the local file system.
+module Puppet::Parser::Functions
+ newfunction(:dirname, :type => :rvalue) do |args|
+ if args[0].is_a?(Array)
+ args.collect do |a| File.dirname(a) end
+ else
+ File.dirname(args[0])
+ end
+ end
+end
+
diff --git a/lib/puppet/parser/functions/gsub.rb b/lib/puppet/parser/functions/gsub.rb
new file mode 100644
index 0000000..e2410ff
--- /dev/null
+++ b/lib/puppet/parser/functions/gsub.rb
@@ -0,0 +1,17 @@
+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/hostname.rb b/lib/puppet/parser/functions/hostname.rb
new file mode 100644
index 0000000..7bc477f
--- /dev/null
+++ b/lib/puppet/parser/functions/hostname.rb
@@ -0,0 +1,13 @@
+# get an uniq array of ipaddresses for a hostname
+require 'resolv'
+
+module Puppet::Parser::Functions
+ newfunction(:hostname, :type => :rvalue) do |args|
+ res = Array.new
+ Resolv::DNS.new.each_address(args[0]){ |addr|
+ res << addr
+ }
+ res.uniq
+ end
+end
+
diff --git a/lib/puppet/parser/functions/prefix_with.rb b/lib/puppet/parser/functions/prefix_with.rb
new file mode 100644
index 0000000..6e64a4a
--- /dev/null
+++ b/lib/puppet/parser/functions/prefix_with.rb
@@ -0,0 +1,9 @@
+# prefix arguments 2..n with first argument
+
+module Puppet::Parser::Functions
+ newfunction(:prefix_with, :type => :rvalue) do |args|
+ prefix = args.shift
+ args.collect {|v| "%s%s" % [prefix, v] }
+ end
+end
+
diff --git a/lib/puppet/parser/functions/re_escape.rb b/lib/puppet/parser/functions/re_escape.rb
new file mode 100644
index 0000000..6e5904b
--- /dev/null
+++ b/lib/puppet/parser/functions/re_escape.rb
@@ -0,0 +1,7 @@
+# apply regexp escaping to a string
+module Puppet::Parser::Functions
+ newfunction(:re_escape, :type => :rvalue) do |args|
+ Regexp.escape(args[0])
+ end
+end
+
diff --git a/lib/puppet/parser/functions/sha1.rb b/lib/puppet/parser/functions/sha1.rb
new file mode 100644
index 0000000..b5aa813
--- /dev/null
+++ b/lib/puppet/parser/functions/sha1.rb
@@ -0,0 +1,9 @@
+# return the sha1 hash
+require 'digest/sha1'
+
+module Puppet::Parser::Functions
+ newfunction(:sha1, :type => :rvalue) do |args|
+ Digest::SHA1.hexdigest(args[0])
+ end
+end
+
diff --git a/lib/puppet/parser/functions/slash_escape.rb b/lib/puppet/parser/functions/slash_escape.rb
new file mode 100644
index 0000000..04d3b95
--- /dev/null
+++ b/lib/puppet/parser/functions/slash_escape.rb
@@ -0,0 +1,7 @@
+# escape slashes in a String
+module Puppet::Parser::Functions
+ newfunction(:slash_escape, :type => :rvalue) do |args|
+ args[0].gsub(/\//, '\\/')
+ end
+end
+
diff --git a/lib/puppet/parser/functions/split.rb b/lib/puppet/parser/functions/split.rb
new file mode 100644
index 0000000..5237c92
--- /dev/null
+++ b/lib/puppet/parser/functions/split.rb
@@ -0,0 +1,17 @@
+# split($string, $delimiter) : $string
+# split($string[], $delimiter) : $string[][]
+#
+# Split the first argument(s) on every $delimiter. $delimiter is interpreted as
+# Ruby regular expression.
+#
+# 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/strlength.rb b/lib/puppet/parser/functions/strlength.rb
new file mode 100644
index 0000000..147b24a
--- /dev/null
+++ b/lib/puppet/parser/functions/strlength.rb
@@ -0,0 +1,6 @@
+module Puppet::Parser::Functions
+ newfunction(:strlength, :type => :rvalue) do |args|
+ args[0].to_s.length
+ end
+end
+
diff --git a/lib/puppet/parser/functions/substitute.rb b/lib/puppet/parser/functions/substitute.rb
new file mode 100644
index 0000000..4c97def
--- /dev/null
+++ b/lib/puppet/parser/functions/substitute.rb
@@ -0,0 +1,20 @@
+# subsititute($string, $regex, $replacement) : $string
+# subsititute($string[], $regex, $replacement) : $string[]
+#
+# Replace all ocurrences of $regex in $string by $replacement.
+# $regex is interpreted as Ruby regular expression.
+#
+# For long-term portability it is recommended to refrain from using Ruby's
+# extended RE features.
+module Puppet::Parser::Functions
+ newfunction(:substitute, :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
+