diff options
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r-- | lib/puppet/parser/functions/defined.rb | 46 | ||||
-rw-r--r-- | lib/puppet/parser/functions/fail.rb | 4 | ||||
-rw-r--r-- | lib/puppet/parser/functions/file.rb | 38 | ||||
-rw-r--r-- | lib/puppet/parser/functions/fqdn_rand.rb | 14 | ||||
-rw-r--r-- | lib/puppet/parser/functions/generate.rb | 48 | ||||
-rw-r--r-- | lib/puppet/parser/functions/include.rb | 32 | ||||
-rw-r--r-- | lib/puppet/parser/functions/inline_template.rb | 32 | ||||
-rw-r--r-- | lib/puppet/parser/functions/realize.rb | 16 | ||||
-rw-r--r-- | lib/puppet/parser/functions/regsubst.rb | 144 | ||||
-rw-r--r-- | lib/puppet/parser/functions/require.rb | 76 | ||||
-rw-r--r-- | lib/puppet/parser/functions/search.rb | 10 | ||||
-rw-r--r-- | lib/puppet/parser/functions/sha1.rb | 4 | ||||
-rw-r--r-- | lib/puppet/parser/functions/shellquote.rb | 62 | ||||
-rw-r--r-- | lib/puppet/parser/functions/split.rb | 22 | ||||
-rw-r--r-- | lib/puppet/parser/functions/sprintf.rb | 16 | ||||
-rw-r--r-- | lib/puppet/parser/functions/tag.rb | 6 | ||||
-rw-r--r-- | lib/puppet/parser/functions/tagged.rb | 26 | ||||
-rw-r--r-- | lib/puppet/parser/functions/template.rb | 36 | ||||
-rw-r--r-- | lib/puppet/parser/functions/versioncmp.rb | 30 |
19 files changed, 331 insertions, 331 deletions
diff --git a/lib/puppet/parser/functions/defined.rb b/lib/puppet/parser/functions/defined.rb index 4d1d8c6fd..2930a65cc 100644 --- a/lib/puppet/parser/functions/defined.rb +++ b/lib/puppet/parser/functions/defined.rb @@ -1,27 +1,27 @@ # Test whether a given class or definition is defined Puppet::Parser::Functions::newfunction(:defined, :type => :rvalue, :doc => "Determine whether a given - type is defined, either as a native type or a defined type, or whether a class is defined. - This is useful for checking whether a class is defined and only including it if it is. - This function can also test whether a resource has been defined, using resource references - (e.g., ``if defined(File['/tmp/myfile']) { ... }``). This function is unfortunately - dependent on the parse order of the configuration when testing whether a resource is defined.") do |vals| - result = false - vals = [vals] unless vals.is_a?(Array) - vals.each do |val| - case val - when String - if Puppet::Type.type(val) or find_definition(val) or find_hostclass(val) - result = true - break - end - when Puppet::Resource - if findresource(val.to_s) - result = true - break - end - else - raise ArgumentError, "Invalid argument of type '#{val.class}' to 'defined'" - end + type is defined, either as a native type or a defined type, or whether a class is defined. + This is useful for checking whether a class is defined and only including it if it is. + This function can also test whether a resource has been defined, using resource references + (e.g., ``if defined(File['/tmp/myfile']) { ... }``). This function is unfortunately + dependent on the parse order of the configuration when testing whether a resource is defined.") do |vals| + result = false + vals = [vals] unless vals.is_a?(Array) + vals.each do |val| + case val + when String + if Puppet::Type.type(val) or find_definition(val) or find_hostclass(val) + result = true + break end - result + when Puppet::Resource + if findresource(val.to_s) + result = true + break + end + else + raise ArgumentError, "Invalid argument of type '#{val.class}' to 'defined'" + end + end + result end diff --git a/lib/puppet/parser/functions/fail.rb b/lib/puppet/parser/functions/fail.rb index 35b20ee92..5bef6c7e3 100644 --- a/lib/puppet/parser/functions/fail.rb +++ b/lib/puppet/parser/functions/fail.rb @@ -1,4 +1,4 @@ Puppet::Parser::Functions::newfunction(:fail, :doc => "Fail with a parse error.") do |vals| - vals = vals.collect { |s| s.to_s }.join(" ") if vals.is_a? Array - raise Puppet::ParseError, vals.to_s + vals = vals.collect { |s| s.to_s }.join(" ") if vals.is_a? Array + raise Puppet::ParseError, vals.to_s end diff --git a/lib/puppet/parser/functions/file.rb b/lib/puppet/parser/functions/file.rb index d13b01ede..963111260 100644 --- a/lib/puppet/parser/functions/file.rb +++ b/lib/puppet/parser/functions/file.rb @@ -1,23 +1,23 @@ # Returns the contents of a file - Puppet::Parser::Functions::newfunction( - :file, :type => :rvalue, + Puppet::Parser::Functions::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 #{vals.join(", ")}" - end + :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 #{vals.join(", ")}" + end end diff --git a/lib/puppet/parser/functions/fqdn_rand.rb b/lib/puppet/parser/functions/fqdn_rand.rb index 27af2d7ca..3e7018ac4 100644 --- a/lib/puppet/parser/functions/fqdn_rand.rb +++ b/lib/puppet/parser/functions/fqdn_rand.rb @@ -1,9 +1,9 @@ Puppet::Parser::Functions::newfunction(:fqdn_rand, :type => :rvalue, :doc => - "Generates random numbers based on the node's fqdn. The first argument - sets the range. Additional (optional) arguments may be used to further - distinguish the seed.") do |args| - require 'md5' - max = args.shift - srand MD5.new([lookupvar('fqdn'),args].join(':')).to_s.hex - rand(max).to_s + "Generates random numbers based on the node's fqdn. The first argument + sets the range. Additional (optional) arguments may be used to further + distinguish the seed.") do |args| + require 'md5' + max = args.shift + srand MD5.new([lookupvar('fqdn'),args].join(':')).to_s.hex + rand(max).to_s end diff --git a/lib/puppet/parser/functions/generate.rb b/lib/puppet/parser/functions/generate.rb index 8430f03a6..91f7b2240 100644 --- a/lib/puppet/parser/functions/generate.rb +++ b/lib/puppet/parser/functions/generate.rb @@ -1,31 +1,31 @@ # Runs an external command and returns the results Puppet::Parser::Functions::newfunction(:generate, :type => :rvalue, - :doc => "Calls an external command on the Puppet master and returns - the results of the command. Any arguments are passed to the external command as - arguments. If the generator does not exit with return code of 0, - the generator is considered to have failed and a parse error is - thrown. Generators can only have file separators, alphanumerics, dashes, - and periods in them. This function will attempt to protect you from - malicious generator calls (e.g., those with '..' in them), but it can - never be entirely safe. No subshell is used to execute - generators, so all shell metacharacters are passed directly to - the generator.") do |args| + :doc => "Calls an external command on the Puppet master and returns + the results of the command. Any arguments are passed to the external command as + arguments. If the generator does not exit with return code of 0, + the generator is considered to have failed and a parse error is + thrown. Generators can only have file separators, alphanumerics, dashes, + and periods in them. This function will attempt to protect you from + malicious generator calls (e.g., those with '..' in them), but it can + never be entirely safe. No subshell is used to execute + generators, so all shell metacharacters are passed directly to + the generator.") do |args| - raise Puppet::ParseError, "Generators must be fully qualified" unless args[0] =~ /^#{File::SEPARATOR}/ + raise Puppet::ParseError, "Generators must be fully qualified" unless args[0] =~ /^#{File::SEPARATOR}/ - unless args[0] =~ /^[-#{File::SEPARATOR}\w.]+$/ - raise Puppet::ParseError, - "Generators can only contain alphanumerics, file separators, and dashes" - end + unless args[0] =~ /^[-#{File::SEPARATOR}\w.]+$/ + raise Puppet::ParseError, + "Generators can only contain alphanumerics, file separators, and dashes" + end - if args[0] =~ /\.\./ - raise Puppet::ParseError, - "Can not use generators with '..' in them." - end + if args[0] =~ /\.\./ + raise Puppet::ParseError, + "Can not use generators with '..' in them." + end - begin - Dir.chdir(File.dirname(args[0])) { Puppet::Util.execute(args) } - rescue Puppet::ExecutionFailure => detail - raise Puppet::ParseError, "Failed to execute generator #{args[0]}: #{detail}" - end + begin + Dir.chdir(File.dirname(args[0])) { Puppet::Util.execute(args) } + rescue Puppet::ExecutionFailure => detail + raise Puppet::ParseError, "Failed to execute generator #{args[0]}: #{detail}" + end end diff --git a/lib/puppet/parser/functions/include.rb b/lib/puppet/parser/functions/include.rb index d1bafa54a..1e12a6a88 100644 --- a/lib/puppet/parser/functions/include.rb +++ b/lib/puppet/parser/functions/include.rb @@ -1,24 +1,24 @@ # Include the specified classes Puppet::Parser::Functions::newfunction(:include, :doc => "Evaluate one or more classes.") do |vals| - vals = [vals] unless vals.is_a?(Array) + vals = [vals] unless vals.is_a?(Array) - # The 'false' disables lazy evaluation. - klasses = compiler.evaluate_classes(vals, self, false) + # The 'false' disables lazy evaluation. + klasses = compiler.evaluate_classes(vals, self, false) - missing = vals.find_all do |klass| - ! klasses.include?(klass) - end + missing = vals.find_all do |klass| + ! klasses.include?(klass) + end - unless missing.empty? - # Throw an error if we didn't evaluate all of the classes. - str = "Could not find class" - str += "es" if missing.length > 1 + unless missing.empty? + # Throw an error if we didn't evaluate all of the classes. + str = "Could not find class" + str += "es" if missing.length > 1 - str += " " + missing.join(", ") + str += " " + missing.join(", ") - if n = namespaces and ! n.empty? and n != [""] - str += " in namespaces #{@namespaces.join(", ")}" - end - self.fail Puppet::ParseError, str - end + if n = namespaces and ! n.empty? and n != [""] + str += " in namespaces #{@namespaces.join(", ")}" + end + self.fail Puppet::ParseError, str + end end diff --git a/lib/puppet/parser/functions/inline_template.rb b/lib/puppet/parser/functions/inline_template.rb index 6c0485d1e..11d980fab 100644 --- a/lib/puppet/parser/functions/inline_template.rb +++ b/lib/puppet/parser/functions/inline_template.rb @@ -1,20 +1,20 @@ Puppet::Parser::Functions::newfunction(:inline_template, :type => :rvalue, :doc => - "Evaluate a template string and return its value. See `the templating docs - </trac/puppet/wiki/PuppetTemplating>`_ for more information. Note that - if multiple template strings are specified, their output is all concatenated - and returned as the output of the function.") do |vals| - require 'erb' + "Evaluate a template string and return its value. See `the templating docs + </trac/puppet/wiki/PuppetTemplating>`_ for more information. Note that + if multiple template strings are specified, their output is all concatenated + and returned as the output of the function.") do |vals| + require 'erb' - vals.collect do |string| - # Use a wrapper, so the template can't get access to the full - # Scope object. + vals.collect do |string| + # Use a wrapper, so the template can't get access to the full + # Scope object. - wrapper = Puppet::Parser::TemplateWrapper.new(self) - begin - wrapper.result(string) - rescue => detail - raise Puppet::ParseError, - "Failed to parse inline template: #{detail}" - end - end.join("") + wrapper = Puppet::Parser::TemplateWrapper.new(self) + begin + wrapper.result(string) + rescue => detail + raise Puppet::ParseError, + "Failed to parse inline template: #{detail}" + end + end.join("") end diff --git a/lib/puppet/parser/functions/realize.rb b/lib/puppet/parser/functions/realize.rb index 6aff19d29..4247b8af8 100644 --- a/lib/puppet/parser/functions/realize.rb +++ b/lib/puppet/parser/functions/realize.rb @@ -2,13 +2,13 @@ # be a good bit faster. Puppet::Parser::Functions::newfunction(:realize, :doc => "Make a virtual object real. This is useful - when you want to know the name of the virtual object and don't want to - bother with a full collection. It is slightly faster than a collection, - and, of course, is a bit shorter. You must pass the object using a - reference; e.g.: ``realize User[luke]``." ) do |vals| - coll = Puppet::Parser::Collector.new(self, :nomatter, nil, nil, :virtual) - vals = [vals] unless vals.is_a?(Array) - coll.resources = vals.flatten + when you want to know the name of the virtual object and don't want to + bother with a full collection. It is slightly faster than a collection, + and, of course, is a bit shorter. You must pass the object using a + reference; e.g.: ``realize User[luke]``." ) do |vals| + coll = Puppet::Parser::Collector.new(self, :nomatter, nil, nil, :virtual) + vals = [vals] unless vals.is_a?(Array) + coll.resources = vals.flatten - compiler.add_collection(coll) + compiler.add_collection(coll) end diff --git a/lib/puppet/parser/functions/regsubst.rb b/lib/puppet/parser/functions/regsubst.rb index c47c1654e..c0aeef222 100644 --- a/lib/puppet/parser/functions/regsubst.rb +++ b/lib/puppet/parser/functions/regsubst.rb @@ -1,10 +1,10 @@ module Puppet::Parser::Functions - newfunction( - :regsubst, :type => :rvalue, + newfunction( + :regsubst, :type => :rvalue, - :doc => " - Perform regexp replacement on a string or array of strings. + :doc => " + Perform regexp replacement on a string or array of strings. - **Parameters** (in order): @@ -16,85 +16,85 @@ module Puppet::Parser::Functions :flags: Optional. String of single letter flags for how the regexp is interpreted: - - **E** Extended regexps - - **I** Ignore case in regexps - - **M** Multiline regexps - - **G** Global replacement; all occurrences of the regexp in each target string will be replaced. Without this, only the first occurrence will be replaced. + - **E** Extended regexps + - **I** Ignore case in regexps + - **M** Multiline regexps + - **G** Global replacement; all occurrences of the regexp in each target string will be replaced. Without this, only the first occurrence will be replaced. :lang: Optional. How to handle multibyte characters. A single-character string with the following values: - - **N** None - - **E** EUC - - **S** SJIS - - **U** UTF-8 + - **N** None + - **E** EUC + - **S** SJIS + - **U** UTF-8 - **Examples** Get the third octet from the node's IP address:: - $i3 = regsubst($ipaddress,'^([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)$','\\3') + $i3 = regsubst($ipaddress,'^([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)$','\\3') Put angle brackets around each octet in the node's IP address:: - $x = regsubst($ipaddress, '([0-9]+)', '<\\1>', 'G') + $x = regsubst($ipaddress, '([0-9]+)', '<\\1>', 'G') ") \ - do |args| - unless args.length.between?(3, 5) - - raise( - Puppet::ParseError, - - "regsubst(): got #{args.length} arguments, expected 3 to 5") - end - target, regexp, replacement, flags, lang = args - reflags = 0 - operation = :sub - if flags == nil - flags = [] - elsif flags.respond_to?(:split) - flags = flags.split('') - else - - raise( - Puppet::ParseError, - - "regsubst(): bad flags parameter #{flags.class}:`#{flags}'") - end - flags.each do |f| - case f - when 'G' then operation = :gsub - when 'E' then reflags |= Regexp::EXTENDED - when 'I' then reflags |= Regexp::IGNORECASE - when 'M' then reflags |= Regexp::MULTILINE - else raise(Puppet::ParseError, "regsubst(): bad flag `#{f}'") - end - end - begin - re = Regexp.compile(regexp, reflags, lang) - rescue RegexpError, TypeError - - raise( - Puppet::ParseError, - - "regsubst(): Bad regular expression `#{regexp}'") - end - if target.respond_to?(operation) - # String parameter -> string result - result = target.send(operation, re, replacement) - elsif target.respond_to?(:collect) and - target.respond_to?(:all?) and - target.all? { |e| e.respond_to?(operation) } - # Array parameter -> array result - result = target.collect { |e| - e.send(operation, re, replacement) - } - else - - raise( - Puppet::ParseError, - - "regsubst(): bad target #{target.class}:`#{target}'") - end - return result + do |args| + unless args.length.between?(3, 5) + + raise( + Puppet::ParseError, + + "regsubst(): got #{args.length} arguments, expected 3 to 5") + end + target, regexp, replacement, flags, lang = args + reflags = 0 + operation = :sub + if flags == nil + flags = [] + elsif flags.respond_to?(:split) + flags = flags.split('') + else + + raise( + Puppet::ParseError, + + "regsubst(): bad flags parameter #{flags.class}:`#{flags}'") + end + flags.each do |f| + case f + when 'G' then operation = :gsub + when 'E' then reflags |= Regexp::EXTENDED + when 'I' then reflags |= Regexp::IGNORECASE + when 'M' then reflags |= Regexp::MULTILINE + else raise(Puppet::ParseError, "regsubst(): bad flag `#{f}'") + end + end + begin + re = Regexp.compile(regexp, reflags, lang) + rescue RegexpError, TypeError + + raise( + Puppet::ParseError, + + "regsubst(): Bad regular expression `#{regexp}'") + end + if target.respond_to?(operation) + # String parameter -> string result + result = target.send(operation, re, replacement) + elsif target.respond_to?(:collect) and + target.respond_to?(:all?) and + target.all? { |e| e.respond_to?(operation) } + # Array parameter -> array result + result = target.collect { |e| + e.send(operation, re, replacement) + } + else + + raise( + Puppet::ParseError, + + "regsubst(): bad target #{target.class}:`#{target}'") end + return result + end end diff --git a/lib/puppet/parser/functions/require.rb b/lib/puppet/parser/functions/require.rb index c5c4c851f..3f98c9523 100644 --- a/lib/puppet/parser/functions/require.rb +++ b/lib/puppet/parser/functions/require.rb @@ -1,9 +1,9 @@ # Requires the specified classes - Puppet::Parser::Functions::newfunction( - :require, + Puppet::Parser::Functions::newfunction( + :require, - :doc =>"Evaluate one or more classes, adding the required class as a dependency. + :doc =>"Evaluate one or more classes, adding the required class as a dependency. The relationship metaparameters work well for specifying relationships between individual resources, but they can be clumsy for specifying @@ -12,47 +12,47 @@ relationships between classes. This function is a superset of the class depends on the required class. Warning: using require in place of include can lead to unwanted dependency cycles. - For instance the following manifest, with 'require' instead of 'include' - would produce a nasty dependence cycle, because notify imposes a before - between File[/foo] and Service[foo]:: + For instance the following manifest, with 'require' instead of 'include' + would produce a nasty dependence cycle, because notify imposes a before + between File[/foo] and Service[foo]:: - class myservice { - service { foo: ensure => running } - } + class myservice { + service { foo: ensure => running } + } - class otherstuff { - include myservice - file { '/foo': notify => Service[foo] } - } + class otherstuff { + include myservice + file { '/foo': notify => Service[foo] } + } Note that this function only works with clients 0.25 and later, and it will fail if used with earlier clients. ") do |vals| - # Verify that the 'include' function is loaded - method = Puppet::Parser::Functions.function(:include) - - send(method, vals) - if resource.metaparam_compatibility_mode? - warning "The 'require' function is only compatible with clients at 0.25 and above; including class but not adding dependency" - else - vals = [vals] unless vals.is_a?(Array) - - vals.each do |klass| - # lookup the class in the scopes - if classobj = find_hostclass(klass) - klass = classobj.name - else - raise Puppet::ParseError, "Could not find class #{klass}" - end - - # This is a bit hackish, in some ways, but it's the only way - # to configure a dependency that will make it to the client. - # The 'obvious' way is just to add an edge in the catalog, - # but that is considered a containment edge, not a dependency - # edge, so it usually gets lost on the client. - ref = Puppet::Resource.new(:class, klass) - resource.set_parameter(:require, [resource[:require]].flatten.compact << ref) - end + # Verify that the 'include' function is loaded + method = Puppet::Parser::Functions.function(:include) + + send(method, vals) + if resource.metaparam_compatibility_mode? + warning "The 'require' function is only compatible with clients at 0.25 and above; including class but not adding dependency" + else + vals = [vals] unless vals.is_a?(Array) + + vals.each do |klass| + # lookup the class in the scopes + if classobj = find_hostclass(klass) + klass = classobj.name + else + raise Puppet::ParseError, "Could not find class #{klass}" + end + + # This is a bit hackish, in some ways, but it's the only way + # to configure a dependency that will make it to the client. + # The 'obvious' way is just to add an edge in the catalog, + # but that is considered a containment edge, not a dependency + # edge, so it usually gets lost on the client. + ref = Puppet::Resource.new(:class, klass) + resource.set_parameter(:require, [resource[:require]].flatten.compact << ref) end + end end diff --git a/lib/puppet/parser/functions/search.rb b/lib/puppet/parser/functions/search.rb index 87dd02d67..8a9c7c8be 100644 --- a/lib/puppet/parser/functions/search.rb +++ b/lib/puppet/parser/functions/search.rb @@ -1,7 +1,7 @@ Puppet::Parser::Functions::newfunction(:search, :doc => "Add another namespace for this class to search. - This allows you to create classes with sets of definitions and add - those classes to another class's search path.") do |vals| - vals.each do |val| - add_namespace(val) - end + This allows you to create classes with sets of definitions and add + those classes to another class's search path.") do |vals| + vals.each do |val| + add_namespace(val) + end end diff --git a/lib/puppet/parser/functions/sha1.rb b/lib/puppet/parser/functions/sha1.rb index 432825ee9..10cc55cfe 100644 --- a/lib/puppet/parser/functions/sha1.rb +++ b/lib/puppet/parser/functions/sha1.rb @@ -1,5 +1,5 @@ Puppet::Parser::Functions::newfunction(:sha1, :type => :rvalue, :doc => "Returns a SHA1 hash value from a provided string.") do |args| - require 'sha1' + require 'sha1' - Digest::SHA1.hexdigest(args[0]) + Digest::SHA1.hexdigest(args[0]) end diff --git a/lib/puppet/parser/functions/shellquote.rb b/lib/puppet/parser/functions/shellquote.rb index 888b9769d..3ddb988f2 100644 --- a/lib/puppet/parser/functions/shellquote.rb +++ b/lib/puppet/parser/functions/shellquote.rb @@ -1,39 +1,39 @@ module Puppet::Parser::Functions - Safe = 'a-zA-Z0-9@%_+=:,./-' # Safe unquoted - Dangerous = '!"`$\\' # Unsafe inside double quotes + Safe = 'a-zA-Z0-9@%_+=:,./-' # Safe unquoted + Dangerous = '!"`$\\' # Unsafe inside double quotes - newfunction(:shellquote, :type => :rvalue, :doc => "\ - Quote and concatenate arguments for use in Bourne shell. + newfunction(:shellquote, :type => :rvalue, :doc => "\ + Quote and concatenate arguments for use in Bourne shell. - Each argument is quoted separately, and then all are concatenated - with spaces. If an argument is an array, the elements of that - array is interpolated within the rest of the arguments; this makes - it possible to have an array of arguments and pass that array to - shellquote instead of having to specify each argument - individually in the call. - ") \ - do |args| + Each argument is quoted separately, and then all are concatenated + with spaces. If an argument is an array, the elements of that + array is interpolated within the rest of the arguments; this makes + it possible to have an array of arguments and pass that array to + shellquote instead of having to specify each argument + individually in the call. + ") \ + do |args| - result = [] - args.flatten.each do |word| - if word.length != 0 and word.count(Safe) == word.length - result << word - elsif word.count(Dangerous) == 0 - result << ('"' + word + '"') - elsif word.count("'") == 0 - result << ("'" + word + "'") - else - r = '"' - word.each_byte do |c| - r += "\\" if Dangerous.include?(c) - r += c.chr - end - r += '"' - result << r - end + result = [] + args.flatten.each do |word| + if word.length != 0 and word.count(Safe) == word.length + result << word + elsif word.count(Dangerous) == 0 + result << ('"' + word + '"') + elsif word.count("'") == 0 + result << ("'" + word + "'") + else + r = '"' + word.each_byte do |c| + r += "\\" if Dangerous.include?(c) + r += c.chr end - - return result.join(" ") + r += '"' + result << r + end end + + return result.join(" ") + end end diff --git a/lib/puppet/parser/functions/split.rb b/lib/puppet/parser/functions/split.rb index 09caa18aa..5d0a9dabc 100644 --- a/lib/puppet/parser/functions/split.rb +++ b/lib/puppet/parser/functions/split.rb @@ -1,17 +1,17 @@ module Puppet::Parser::Functions - newfunction( - :split, :type => :rvalue, + newfunction( + :split, :type => :rvalue, - :doc => "\ + :doc => "\ Split a string variable into an array using the specified split regexp. - Usage:: + Usage:: - $string = 'v1.v2:v3.v4' - $array_var1 = split($string, ':') - $array_var2 = split($string, '[.]') - $array_var3 = split($string, '[.:]') + $string = 'v1.v2:v3.v4' + $array_var1 = split($string, ':') + $array_var2 = split($string, '[.]') + $array_var3 = split($string, '[.:]') $array_var1 now holds the result ['v1.v2', 'v3.v4'], while $array_var2 holds ['v1', 'v2:v3', 'v4'], and @@ -22,8 +22,8 @@ a regexp meta-character (.), and that needs protection. A simple way to do that for a single character is to enclose it in square brackets.") do |args| - raise Puppet::ParseError, ("split(): wrong number of arguments (#{args.length}; must be 2)") if args.length != 2 + raise Puppet::ParseError, ("split(): wrong number of arguments (#{args.length}; must be 2)") if args.length != 2 - return args[0].split(Regexp.compile(args[1])) - end + return args[0].split(Regexp.compile(args[1])) + end end diff --git a/lib/puppet/parser/functions/sprintf.rb b/lib/puppet/parser/functions/sprintf.rb index af0a7213e..5ada0fed7 100644 --- a/lib/puppet/parser/functions/sprintf.rb +++ b/lib/puppet/parser/functions/sprintf.rb @@ -1,13 +1,13 @@ module Puppet::Parser::Functions - newfunction( - :sprintf, :type => :rvalue, + newfunction( + :sprintf, :type => :rvalue, - :doc => "Perform printf-style formatting of text. + :doc => "Perform printf-style formatting of text. - The first parameter is format string describing how the rest of the parameters should be formatted. See the documentation for the ``Kernel::sprintf`` function in Ruby for all the details.") do |args| - raise Puppet::ParseError, 'sprintf() needs at least one argument' if args.length < 1 - fmt = args.shift - return sprintf(fmt, *args) - end + The first parameter is format string describing how the rest of the parameters should be formatted. See the documentation for the ``Kernel::sprintf`` function in Ruby for all the details.") do |args| + raise Puppet::ParseError, 'sprintf() needs at least one argument' if args.length < 1 + fmt = args.shift + return sprintf(fmt, *args) + end end diff --git a/lib/puppet/parser/functions/tag.rb b/lib/puppet/parser/functions/tag.rb index 3e487feaf..84df175eb 100644 --- a/lib/puppet/parser/functions/tag.rb +++ b/lib/puppet/parser/functions/tag.rb @@ -1,6 +1,6 @@ # Tag the current scope with each passed name Puppet::Parser::Functions::newfunction(:tag, :doc => "Add the specified tags to the containing class - or definition. All contained objects will then acquire that tag, also. - ") do |vals| - self.resource.tag(*vals) + or definition. All contained objects will then acquire that tag, also. + ") do |vals| + self.resource.tag(*vals) end diff --git a/lib/puppet/parser/functions/tagged.rb b/lib/puppet/parser/functions/tagged.rb index fccb13205..aaa2adfad 100644 --- a/lib/puppet/parser/functions/tagged.rb +++ b/lib/puppet/parser/functions/tagged.rb @@ -1,18 +1,18 @@ # Test whether a given tag is set. This functions as a big OR -- if any of the specified tags are unset, we return false. Puppet::Parser::Functions::newfunction(:tagged, :type => :rvalue, :doc => "A boolean function that - tells you whether the current container is tagged with the specified tags. - The tags are ANDed, so that all of the specified tags must be included for - the function to return true.") do |vals| - configtags = compiler.catalog.tags - resourcetags = resource.tags + tells you whether the current container is tagged with the specified tags. + The tags are ANDed, so that all of the specified tags must be included for + the function to return true.") do |vals| + configtags = compiler.catalog.tags + resourcetags = resource.tags - retval = true - vals.each do |val| - unless configtags.include?(val) or resourcetags.include?(val) - retval = false - break - end - end + retval = true + vals.each do |val| + unless configtags.include?(val) or resourcetags.include?(val) + retval = false + break + end + end - return retval + return retval end diff --git a/lib/puppet/parser/functions/template.rb b/lib/puppet/parser/functions/template.rb index 6c4873efe..f51bcc1e2 100644 --- a/lib/puppet/parser/functions/template.rb +++ b/lib/puppet/parser/functions/template.rb @@ -1,22 +1,22 @@ Puppet::Parser::Functions::newfunction(:template, :type => :rvalue, :doc => - "Evaluate a template and return its value. See `the templating docs - <http://docs.puppetlabs.com/guides/templating.html>`_ for more information. - Note that if multiple templates are specified, their output is all - concatenated and returned as the output of the function.") do |vals| - require 'erb' + "Evaluate a template and return its value. See `the templating docs + <http://docs.puppetlabs.com/guides/templating.html>`_ for more information. + Note that if multiple templates are specified, their output is all + concatenated and returned as the output of the function.") do |vals| + require 'erb' - vals.collect do |file| - # Use a wrapper, so the template can't get access to the full - # Scope object. - debug "Retrieving template #{file}" + vals.collect do |file| + # Use a wrapper, so the template can't get access to the full + # Scope object. + debug "Retrieving template #{file}" - wrapper = Puppet::Parser::TemplateWrapper.new(self) - wrapper.file = file - begin - wrapper.result - rescue => detail - raise Puppet::ParseError, - "Failed to parse template #{file}: #{detail}" - end - end.join("") + wrapper = Puppet::Parser::TemplateWrapper.new(self) + wrapper.file = file + begin + wrapper.result + rescue => detail + raise Puppet::ParseError, + "Failed to parse template #{file}: #{detail}" + end + end.join("") end diff --git a/lib/puppet/parser/functions/versioncmp.rb b/lib/puppet/parser/functions/versioncmp.rb index b8d39af96..b38406532 100644 --- a/lib/puppet/parser/functions/versioncmp.rb +++ b/lib/puppet/parser/functions/versioncmp.rb @@ -1,34 +1,34 @@ require 'puppet/util/package' - Puppet::Parser::Functions::newfunction( - :versioncmp, :type => :rvalue, + Puppet::Parser::Functions::newfunction( + :versioncmp, :type => :rvalue, - :doc => "Compares two versions + :doc => "Compares two versions Prototype:: - \$result = versioncmp(a, b) + \$result = versioncmp(a, b) - where a and b are arbitrary version strings + where a and b are arbitrary version strings This functions returns a number:: - * > 0 if version a is greater than version b - * == 0 if both version are equals - * < 0 if version a is less than version b + * > 0 if version a is greater than version b + * == 0 if both version are equals + * < 0 if version a is less than version b Example:: - if versioncmp('2.6-1', '2.4.5') > 0 { - notice('2.6-1 is > than 2.4.5') - } + if versioncmp('2.6-1', '2.4.5') > 0 { + notice('2.6-1 is > than 2.4.5') + } ") do |args| - unless args.length == 2 - raise Puppet::ParseError, "versioncmp should have 2 arguments" - end + unless args.length == 2 + raise Puppet::ParseError, "versioncmp should have 2 arguments" + end - return Puppet::Util::Package.versioncmp(args[0], args[1]) + return Puppet::Util::Package.versioncmp(args[0], args[1]) end |