Skip to content

Commit

Permalink
M #-: VR: Further improve the load_env parser
Browse files Browse the repository at this point in the history
This handles cases where extra empty lines and line breaks are inserted.
  • Loading branch information
sk4zuzu committed May 14, 2024
1 parent 5fabd92 commit b7d496c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
31 changes: 14 additions & 17 deletions appliances/lib/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,27 @@ def load_env(path = '/run/one-context/one_env')
# literal newlines!
folded = Enumerator.new do |y|
cached = []
File.read(path).lines.each do |line|
if (stripped = line.strip).end_with?(%["])
cached << stripped
y << cached.join

yield_prev_line = -> do
unless cached.empty?
y << cached.join.gsub(/./m, replacements)
cached = []
else
escaped = line.gsub(/./m, replacements)
cached << escaped
end
end
end

folded.each do |line|
line.strip!
next if line.empty?

next unless line.start_with?(%[export ]) && line.end_with?(%["])
File.read(path).lines.each do |line|
yield_prev_line.call if line =~ /^export [^=]+="/
cached << line
end

line.delete_prefix!(%[export ])
yield_prev_line.call
end

k, v = line.split(%[=], 2)
next if v.nil?
folded.each do |line|
# Everything to the right of the last " is discarded!
next unless line =~ /^export ([^=]+)=(".*")[^"]*$/

ENV[k] = v.undump
ENV[$1] = $2.undump
end
end

Expand Down
21 changes: 21 additions & 0 deletions appliances/lib/tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@
export E2="A
B\nC"
INPUT
],
[ { :E1 => "\nA\nB\n",
:E2 => "\nA\nB",
:E3 => "A\n\nB\n" },
<<~'INPUT'
export E1="
A
B
"
export E2="
A
B"
export E3="A
B
"
INPUT
]
]
Dir.mktmpdir do |dir|
Expand Down

0 comments on commit b7d496c

Please sign in to comment.