Skip to content

Commit

Permalink
Modernize escape functions
Browse files Browse the repository at this point in the history
  • Loading branch information
smortex committed Apr 28, 2022
1 parent 23fdf23 commit 78508de
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 110 deletions.
31 changes: 31 additions & 0 deletions lib/puppet/functions/batch_escape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# @summary
# Escapes a string so that it can be safely used in a batch shell command line.
#
# >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single
# quotes.
Puppet::Functions.create_function(:batch_escape) do
# @param string
# The string to escape
#
# @return
# An escaped string that can be safely used in a batch command line.
dispatch :batch_escape do
param 'Any', :string
end

def batch_escape(string)
result = ''

string.to_s.chars.each do |char|
result += case char
when '"' then '""'
when '$', '\\' then "\\#{char}"
else char
end
end

%("#{result}")
end
end
31 changes: 31 additions & 0 deletions lib/puppet/functions/powershell_escape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# @summary
# Escapes a string so that it can be safely used in a PowerShell command line.
#
# >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single
# quotes.
Puppet::Functions.create_function(:powershell_escape) do
# @param string
# The string to escape
#
# @return
# An escaped string that can be safely used in a PowerShell command line.
dispatch :powershell_escape do
param 'Any', :string
end

def powershell_escape(string)
result = ''

string.to_s.chars.each do |char|
result += case char
when ' ', "'", '`', '|', "\n", '$' then "`#{char}"
when '"' then '\`"'
else char
end
end

result
end
end
25 changes: 25 additions & 0 deletions lib/puppet/functions/shell_escape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# @summary
# Escapes a string so that it can be safely used in a Bourne shell command line.
#
# >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single
# quotes.
#
# This function behaves the same as ruby's Shellwords.shellescape() function.
Puppet::Functions.create_function(:shell_escape) do
# @param string
# The string to escape
#
# @return
# An escaped string that can be safely used in a Bourne shell command line.
dispatch :shell_escape do
param 'Any', :string
end

def shell_escape(string)
require 'shellwords'

Shellwords.shellescape(string.to_s)
end
end
36 changes: 0 additions & 36 deletions lib/puppet/parser/functions/batch_escape.rb

This file was deleted.

36 changes: 0 additions & 36 deletions lib/puppet/parser/functions/powershell_escape.rb

This file was deleted.

32 changes: 0 additions & 32 deletions lib/puppet/parser/functions/shell_escape.rb

This file was deleted.

4 changes: 2 additions & 2 deletions spec/functions/batch_escape_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
it { is_expected.not_to eq(nil) }

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

describe 'stringification' do
Expand Down
4 changes: 2 additions & 2 deletions spec/functions/powershell_escape_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
it { is_expected.not_to eq(nil) }

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

describe 'stringification' do
Expand Down
4 changes: 2 additions & 2 deletions spec/functions/shell_escape_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
it { is_expected.not_to eq(nil) }

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

describe 'stringification' do
Expand Down

0 comments on commit 78508de

Please sign in to comment.