Skip to content

Commit

Permalink
Fix save_to_file to be indentation independent
Browse files Browse the repository at this point in the history
  • Loading branch information
ShimShtein committed Aug 20, 2024
1 parent 44160c0 commit f464a5b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions app/services/foreman/renderer/scope/macros/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,21 @@ def pxe_kernel_options
example "save_to_file(shell_escape('/tmp/a file with spaces'), nil) # => 'cp /dev/null /tmp/a\ file\ with\ spaces'"
end
def save_to_file(filename, content, verbatim: false)
delimiter = 'EOF-' + Digest::SHA512.hexdigest(filename)[0..7]
if content.empty?
"cp /dev/null #{filename}"
elsif verbatim
content = Base64.encode64(content)
"cat << #{delimiter} | base64 -d > #{filename}\n#{content}#{delimiter}"
# since content is a base64 string we don't need to escape it
"echo #{content} | base64 -d > #{filename}"
else
content += "\n" unless content.end_with?("\n")
"cat << #{delimiter} > #{filename}\n#{content}#{delimiter}"
content_echos = content.split("\n").map do |content_line|
# to make sure there the substitutions are working, have the bash script
# handle all special characters except double quotes.
"echo \"#{content_line.gsub('"', '\"')}\" >> #{filename}"
end.join("\n")

# prefix the append commands with a cleanup command
"> #{filename}\n#{content_echos}"
end
end

Expand Down

0 comments on commit f464a5b

Please sign in to comment.