Skip to content

Commit 25b0fbd

Browse files
authored
Merge pull request #1238 from smortex/modernize-escape-functions
Modernize escape functions
2 parents f04feb8 + 78508de commit 25b0fbd

9 files changed

+93
-110
lines changed

Diff for: lib/puppet/functions/batch_escape.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Escapes a string so that it can be safely used in a batch shell command line.
5+
#
6+
# >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single
7+
# quotes.
8+
Puppet::Functions.create_function(:batch_escape) do
9+
# @param string
10+
# The string to escape
11+
#
12+
# @return
13+
# An escaped string that can be safely used in a batch command line.
14+
dispatch :batch_escape do
15+
param 'Any', :string
16+
end
17+
18+
def batch_escape(string)
19+
result = ''
20+
21+
string.to_s.chars.each do |char|
22+
result += case char
23+
when '"' then '""'
24+
when '$', '\\' then "\\#{char}"
25+
else char
26+
end
27+
end
28+
29+
%("#{result}")
30+
end
31+
end

Diff for: lib/puppet/functions/powershell_escape.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Escapes a string so that it can be safely used in a PowerShell command line.
5+
#
6+
# >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single
7+
# quotes.
8+
Puppet::Functions.create_function(:powershell_escape) do
9+
# @param string
10+
# The string to escape
11+
#
12+
# @return
13+
# An escaped string that can be safely used in a PowerShell command line.
14+
dispatch :powershell_escape do
15+
param 'Any', :string
16+
end
17+
18+
def powershell_escape(string)
19+
result = ''
20+
21+
string.to_s.chars.each do |char|
22+
result += case char
23+
when ' ', "'", '`', '|', "\n", '$' then "`#{char}"
24+
when '"' then '\`"'
25+
else char
26+
end
27+
end
28+
29+
result
30+
end
31+
end

Diff for: lib/puppet/functions/shell_escape.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Escapes a string so that it can be safely used in a Bourne shell command line.
5+
#
6+
# >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single
7+
# quotes.
8+
#
9+
# This function behaves the same as ruby's Shellwords.shellescape() function.
10+
Puppet::Functions.create_function(:shell_escape) do
11+
# @param string
12+
# The string to escape
13+
#
14+
# @return
15+
# An escaped string that can be safely used in a Bourne shell command line.
16+
dispatch :shell_escape do
17+
param 'Any', :string
18+
end
19+
20+
def shell_escape(string)
21+
require 'shellwords'
22+
23+
Shellwords.shellescape(string.to_s)
24+
end
25+
end

Diff for: lib/puppet/parser/functions/batch_escape.rb

-36
This file was deleted.

Diff for: lib/puppet/parser/functions/powershell_escape.rb

-36
This file was deleted.

Diff for: lib/puppet/parser/functions/shell_escape.rb

-32
This file was deleted.

Diff for: spec/functions/batch_escape_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
it { is_expected.not_to eq(nil) }
77

88
describe 'signature validation' do
9-
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
10-
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
9+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'batch_escape' expects 1 argument, got none}) }
10+
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(ArgumentError, %r{'batch_escape' expects 1 argument, got 2}) }
1111
end
1212

1313
describe 'stringification' do

Diff for: spec/functions/powershell_escape_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
it { is_expected.not_to eq(nil) }
77

88
describe 'signature validation' do
9-
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
10-
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
9+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'powershell_escape' expects 1 argument, got none}) }
10+
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(ArgumentError, %r{'powershell_escape' expects 1 argument, got 2}) }
1111
end
1212

1313
describe 'stringification' do

Diff for: spec/functions/shell_escape_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
it { is_expected.not_to eq(nil) }
77

88
describe 'signature validation' do
9-
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
10-
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
9+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'shell_escape' expects 1 argument, got none}) }
10+
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(ArgumentError, %r{'shell_escape' expects 1 argument, got 2}) }
1111
end
1212

1313
describe 'stringification' do

0 commit comments

Comments
 (0)