From 97e15ea5a84bf414529e110a4215d9fe7f9f20e0 Mon Sep 17 00:00:00 2001 From: mh Date: Sat, 31 Oct 2009 15:16:37 +0100 Subject: move plugin directory to fit new 0.25 style --- lib/puppet/parser/functions/basename.rb | 16 ++++++++++++++++ lib/puppet/parser/functions/dirname.rb | 16 ++++++++++++++++ lib/puppet/parser/functions/gsub.rb | 17 +++++++++++++++++ lib/puppet/parser/functions/hostname.rb | 13 +++++++++++++ lib/puppet/parser/functions/prefix_with.rb | 9 +++++++++ lib/puppet/parser/functions/re_escape.rb | 7 +++++++ lib/puppet/parser/functions/sha1.rb | 9 +++++++++ lib/puppet/parser/functions/slash_escape.rb | 7 +++++++ lib/puppet/parser/functions/split.rb | 17 +++++++++++++++++ lib/puppet/parser/functions/strlength.rb | 6 ++++++ lib/puppet/parser/functions/substitute.rb | 20 ++++++++++++++++++++ 11 files changed, 137 insertions(+) create mode 100644 lib/puppet/parser/functions/basename.rb create mode 100644 lib/puppet/parser/functions/dirname.rb create mode 100644 lib/puppet/parser/functions/gsub.rb create mode 100644 lib/puppet/parser/functions/hostname.rb create mode 100644 lib/puppet/parser/functions/prefix_with.rb create mode 100644 lib/puppet/parser/functions/re_escape.rb create mode 100644 lib/puppet/parser/functions/sha1.rb create mode 100644 lib/puppet/parser/functions/slash_escape.rb create mode 100644 lib/puppet/parser/functions/split.rb create mode 100644 lib/puppet/parser/functions/strlength.rb create mode 100644 lib/puppet/parser/functions/substitute.rb (limited to 'lib') 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 + -- cgit v1.2.3 From 63322aa30578c69772d2f1e266a8534b12874a0f Mon Sep 17 00:00:00 2001 From: Jerome Charaoui Date: Fri, 24 Sep 2010 14:41:07 -0400 Subject: Add multi_source_template function --- .../parser/functions/multi_source_template.rb | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/puppet/parser/functions/multi_source_template.rb (limited to 'lib') diff --git a/lib/puppet/parser/functions/multi_source_template.rb b/lib/puppet/parser/functions/multi_source_template.rb new file mode 100644 index 0000000..e075320 --- /dev/null +++ b/lib/puppet/parser/functions/multi_source_template.rb @@ -0,0 +1,29 @@ +module Puppet::Parser::Functions + require 'erb' + + newfunction(:multi_source_template, :type => :rvalue) do |args| + contents = nil + environment = compiler.environment + sources = args + + sources.each do |file| + Puppet.debug("Looking for #{file} in #{environment}") + if filename = Puppet::Parser::Files.find_template(file, environment.to_s) + wrapper = Puppet::Parser::TemplateWrapper.new(self) + wrapper.file = file + + begin + contents = wrapper.result + rescue => detail + raise Puppet::ParseError, "Failed to parse template %s: %s" % [file, detail] + end + + break + end + end + + raise Puppet::ParseError, "multi_source_template: No match found for files: #{sources.join(', ')}" if contents == nil + + contents + end +end -- cgit v1.2.3 From 90a172d01f13924954384229655b5098db0cbb73 Mon Sep 17 00:00:00 2001 From: mh Date: Thu, 21 Oct 2010 00:04:25 +0200 Subject: remove some legacy functions that are now in upstream --- lib/puppet/parser/functions/sha1.rb | 9 --------- lib/puppet/parser/functions/split.rb | 17 ----------------- 2 files changed, 26 deletions(-) delete mode 100644 lib/puppet/parser/functions/sha1.rb delete mode 100644 lib/puppet/parser/functions/split.rb (limited to 'lib') diff --git a/lib/puppet/parser/functions/sha1.rb b/lib/puppet/parser/functions/sha1.rb deleted file mode 100644 index b5aa813..0000000 --- a/lib/puppet/parser/functions/sha1.rb +++ /dev/null @@ -1,9 +0,0 @@ -# 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/split.rb b/lib/puppet/parser/functions/split.rb deleted file mode 100644 index 5237c92..0000000 --- a/lib/puppet/parser/functions/split.rb +++ /dev/null @@ -1,17 +0,0 @@ -# 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 -- cgit v1.2.3 From 2dd17923bf6787029aee00d0b0e1e8dfbb0a5e7f Mon Sep 17 00:00:00 2001 From: mh Date: Thu, 21 Oct 2010 00:10:28 +0200 Subject: add a join function --- lib/puppet/parser/functions/join.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 lib/puppet/parser/functions/join.rb (limited to 'lib') diff --git a/lib/puppet/parser/functions/join.rb b/lib/puppet/parser/functions/join.rb new file mode 100644 index 0000000..95b664c --- /dev/null +++ b/lib/puppet/parser/functions/join.rb @@ -0,0 +1,10 @@ +Puppet::Parser::Functions::newfunction( + :join, + :type => :rvalue, + :doc => "Joins the values of the array in arg1 with the string in arg2 + + Example: join(['a','b'],',') -> 'a,b'" +) do |args| + raise Puppet::ParseError, 'join() needs two arguments' if args.length != 2 + args[0].to_a.join(args[1]) +end -- cgit v1.2.3 From 82eed89bccfe1af07caf925ce91e43b878bfda41 Mon Sep 17 00:00:00 2001 From: mh Date: Thu, 21 Oct 2010 00:40:23 +0200 Subject: add a function to do some array work --- lib/puppet/parser/functions/uniq_flatten.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 lib/puppet/parser/functions/uniq_flatten.rb (limited to 'lib') diff --git a/lib/puppet/parser/functions/uniq_flatten.rb b/lib/puppet/parser/functions/uniq_flatten.rb new file mode 100644 index 0000000..a4cae40 --- /dev/null +++ b/lib/puppet/parser/functions/uniq_flatten.rb @@ -0,0 +1,10 @@ +Puppet::Parser::Functions::newfunction( + :uniq_flatten, + :type => :rvalue, + :doc => "Flattens an array and make it uniq + + Example: uniq_flatten([['a','b'],'a']) -> ['a','b']" +) do |args| + raise Puppet::ParseError, 'uniq_flatten() needs one arguments' if args.length != 1 + args[0].to_a.flatten.uniq +end -- cgit v1.2.3 From 218c68752e3096b79cc9dc2a8749b9776715ade8 Mon Sep 17 00:00:00 2001 From: mh Date: Thu, 21 Oct 2010 01:05:22 +0200 Subject: go on uniq strings Looks like puppet has some internal representation which is not comparable. --- lib/puppet/parser/functions/uniq_flatten.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/puppet/parser/functions/uniq_flatten.rb b/lib/puppet/parser/functions/uniq_flatten.rb index a4cae40..4841c4d 100644 --- a/lib/puppet/parser/functions/uniq_flatten.rb +++ b/lib/puppet/parser/functions/uniq_flatten.rb @@ -6,5 +6,5 @@ Puppet::Parser::Functions::newfunction( Example: uniq_flatten([['a','b'],'a']) -> ['a','b']" ) do |args| raise Puppet::ParseError, 'uniq_flatten() needs one arguments' if args.length != 1 - args[0].to_a.flatten.uniq + args[0].to_a.flatten.collect(&:to_s).uniq end -- cgit v1.2.3 From ba430647940c1cfbf3e220f032804da325da94a7 Mon Sep 17 00:00:00 2001 From: mh Date: Thu, 28 Oct 2010 00:19:44 +0200 Subject: add a new function & tests for that function --- lib/puppet/parser/functions/array_del.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 lib/puppet/parser/functions/array_del.rb (limited to 'lib') diff --git a/lib/puppet/parser/functions/array_del.rb b/lib/puppet/parser/functions/array_del.rb new file mode 100644 index 0000000..e604916 --- /dev/null +++ b/lib/puppet/parser/functions/array_del.rb @@ -0,0 +1,11 @@ +Puppet::Parser::Functions::newfunction( + :array_del, + :type => :rvalue, + :doc => "Deletes items from an array + + Example: array_del(['a','b'],'b') -> ['a']" +) do |args| + raise Puppet::ParseError, 'array_del() needs two arguments' if args.length != 2 + (res=args[0].dup).to_a.delete(args[1]) + res +end -- cgit v1.2.3