diff --git a/lib/puppet/functions/batch_escape.rb b/lib/puppet/functions/batch_escape.rb new file mode 100644 index 000000000..a2015b284 --- /dev/null +++ b/lib/puppet/functions/batch_escape.rb @@ -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 diff --git a/lib/puppet/functions/powershell_escape.rb b/lib/puppet/functions/powershell_escape.rb new file mode 100644 index 000000000..af1be0950 --- /dev/null +++ b/lib/puppet/functions/powershell_escape.rb @@ -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 diff --git a/lib/puppet/functions/shell_escape.rb b/lib/puppet/functions/shell_escape.rb new file mode 100644 index 000000000..db9c1fca6 --- /dev/null +++ b/lib/puppet/functions/shell_escape.rb @@ -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 diff --git a/lib/puppet/parser/functions/batch_escape.rb b/lib/puppet/parser/functions/batch_escape.rb deleted file mode 100644 index 53fc3491c..000000000 --- a/lib/puppet/parser/functions/batch_escape.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -# -# batch_escape.rb -# -module Puppet::Parser::Functions - newfunction(:batch_escape, type: :rvalue, doc: <<-DOC - @summary - Escapes a string so that it can be safely used in a batch shell command line. - - @return - A string of characters with special characters converted to their escaped form. - - >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single - quotes. - DOC - ) do |arguments| - raise(Puppet::ParseError, "batch_escape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 - - string = arguments[0].to_s - - result = '' - - string.chars.each do |char| - result += case char - when '"' then '""' - when '$', '\\' then "\\#{char}" - else char - end - end - - return %("#{result}") - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/powershell_escape.rb b/lib/puppet/parser/functions/powershell_escape.rb deleted file mode 100644 index 44e5f0cc7..000000000 --- a/lib/puppet/parser/functions/powershell_escape.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -# -# powershell_escape.rb -# -module Puppet::Parser::Functions - newfunction(:powershell_escape, type: :rvalue, doc: <<-DOC - @summary - Escapes a string so that it can be safely used in a PowerShell command line. - - @return - A string of characters with special characters converted to their escaped form. - - >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single - quotes. - DOC - ) do |arguments| - raise(Puppet::ParseError, "shell_escape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 - - string = arguments[0].to_s - - result = '' - - string.chars.each do |char| - result += case char - when ' ', "'", '`', '|', "\n", '$' then "`#{char}" - when '"' then '\`"' - else char - end - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/shell_escape.rb b/lib/puppet/parser/functions/shell_escape.rb deleted file mode 100644 index 113feee5d..000000000 --- a/lib/puppet/parser/functions/shell_escape.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -require 'shellwords' -# -# shell_escape.rb -# -module Puppet::Parser::Functions - newfunction(:shell_escape, type: :rvalue, doc: <<-DOC - @summary - Escapes a string so that it can be safely used in a Bourne shell command line. - - @return - A string of characters with metacharacters converted to their escaped form. - - >* 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. - DOC - ) do |arguments| - raise(Puppet::ParseError, "shell_escape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 - - # explicit conversion to string is required for ruby 1.9 - string = arguments[0].to_s - - result = Shellwords.shellescape(string) - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/spec/functions/batch_escape_spec.rb b/spec/functions/batch_escape_spec.rb index b10b7019f..9feca261f 100644 --- a/spec/functions/batch_escape_spec.rb +++ b/spec/functions/batch_escape_spec.rb @@ -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 diff --git a/spec/functions/powershell_escape_spec.rb b/spec/functions/powershell_escape_spec.rb index c108362a6..20c772888 100644 --- a/spec/functions/powershell_escape_spec.rb +++ b/spec/functions/powershell_escape_spec.rb @@ -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 diff --git a/spec/functions/shell_escape_spec.rb b/spec/functions/shell_escape_spec.rb index 4b1723777..2d4c0c094 100644 --- a/spec/functions/shell_escape_spec.rb +++ b/spec/functions/shell_escape_spec.rb @@ -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