Skip to content

Commit 165caa8

Browse files
author
Peter Souter
committed
(MODULES-1582) Initial spike for % placeholder
This simply `gsub`'s the file path into where the % placeholder is.
1 parent 696c89d commit 165caa8

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/puppet/parser/functions/validate_cmd.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ module Puppet::Parser::Functions
66
Perform validation of a string with an external command.
77
The first argument of this function should be a string to
88
test, and the second argument should be a path to a test command
9-
taking a file as last argument. If the command, launched against
10-
a tempfile containing the passed string, returns a non-null value,
11-
compilation will abort with a parse error.
9+
taking a % as a placeholder for the file path (will default to the end).
10+
If the command, launched against a tempfile containing the passed string,
11+
returns a non-null value, compilation will abort with a parse error.
1212
1313
If a third argument is specified, this will be the error message raised and
1414
seen by the user.
@@ -17,8 +17,12 @@ module Puppet::Parser::Functions
1717
1818
Example:
1919
20+
# Defaults to end of path
2021
validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content')
2122
23+
# % as file location
24+
validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to validate config content')
25+
2226
ENDHEREDOC
2327
if (args.length < 2) or (args.length > 3) then
2428
raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)")
@@ -34,10 +38,17 @@ module Puppet::Parser::Functions
3438
begin
3539
tmpfile.write(content)
3640
tmpfile.close
41+
42+
if checkscript.include?('%')
43+
check_with_correct_location = checkscript.gsub(/%/,tmpfile.path)
44+
else
45+
check_with_correct_location = "#{checkscript} #{tmpfile.path}"
46+
end
47+
3748
if Puppet::Util::Execution.respond_to?('execute')
38-
Puppet::Util::Execution.execute("#{checkscript} #{tmpfile.path}")
49+
Puppet::Util::Execution.execute(check_with_correct_location)
3950
else
40-
Puppet::Util.execute("#{checkscript} #{tmpfile.path}")
51+
Puppet::Util.execute(check_with_correct_location)
4152
end
4253
rescue Puppet::ExecutionFailure => detail
4354
msg += "\n#{detail}"

0 commit comments

Comments
 (0)