Cleanup escaping logic

This commit is contained in:
David Heinemeier Hansson
2023-02-20 18:20:08 +01:00
parent 9b02a7668d
commit 1fccaf60b2

View File

@@ -1,13 +1,14 @@
module Mrsk::Utils module Mrsk::Utils
extend self extend self
# Return a list of shell arguments using the same named argument against the passed attributes (hash or array). # Return a list of escaped shell arguments using the same named argument against the passed attributes (hash or array).
def argumentize(argument, attributes, redacted: false) def argumentize(argument, attributes, redacted: false)
Array(attributes).flat_map do |k, v| Array(attributes).flat_map do |key, value|
if v.present? if value.present?
[ argument, redacted ? redact("#{k}=#{escape_bash_string v.to_s}") : "#{k}=#{escape_bash_string v.to_s}" ] escaped_pair = [ key, value.to_s.dump.gsub(/`/, '\\\\`') ].join("=")
[ argument, redacted ? redact(escaped_pair) : escaped_pair ]
else else
[ argument, k ] [ argument, key ]
end end
end end
end end
@@ -26,8 +27,4 @@ module Mrsk::Utils
def redact(arg) # Used in execute_command to hide redact() args a user passes in def redact(arg) # Used in execute_command to hide redact() args a user passes in
arg.to_s.extend(SSHKit::Redaction) # to_s due to our inability to extend Integer, etc arg.to_s.extend(SSHKit::Redaction) # to_s due to our inability to extend Integer, etc
end end
def escape_bash_string(string)
string.dump.gsub(/`/, '\\\\`')
end
end end