From f456104d52a3d6c2597ddb156c512c36a2f3ad04 Mon Sep 17 00:00:00 2001 From: Michal Opala Date: Tue, 14 May 2024 18:03:30 +0200 Subject: [PATCH] M #-: VR: Further improve the load_env parser (#100) This handles cases where extra empty lines and line breaks are inserted. --- appliances/lib/helpers.rb | 31 ++++++++++++++----------------- appliances/lib/tests.rb | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/appliances/lib/helpers.rb b/appliances/lib/helpers.rb index c6f56151..2e554f8e 100644 --- a/appliances/lib/helpers.rb +++ b/appliances/lib/helpers.rb @@ -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 diff --git a/appliances/lib/tests.rb b/appliances/lib/tests.rb index 20653423..5ed31bda 100644 --- a/appliances/lib/tests.rb +++ b/appliances/lib/tests.rb @@ -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|