From b66355a8f74b43c577d6571191f9b41871615d56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= <karel.koci@nic.cz>
Date: Tue, 4 Aug 2020 09:29:44 +0200
Subject: [PATCH] lists/bootstrap: fix invalid usage of environment variables

This fixes bug that caused normal medkits to not be generated as
contract with no name was not located.

The reason is that Lua's string when they are empty are still true
(contrary to other languages such as Python). This adds explicit checks
if they are non-empty as that is more common and more expected.

This also fixes potential problem when some environment variable is not
defined. This does no happen with our script but can happen if that
script is not used.
---
 helpers/medkit-updater-ng.lua |  2 +-
 lists/bootstrap.lua           | 43 +++++++++++++++++++++--------------
 2 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/helpers/medkit-updater-ng.lua b/helpers/medkit-updater-ng.lua
index 809b3a5e4..e76ffbb2c 100644
--- a/helpers/medkit-updater-ng.lua
+++ b/helpers/medkit-updater-ng.lua
@@ -4,7 +4,7 @@ Root script for updater-ng configuration used for medkit generation.
 
 -- Get target board
 model = os.getenv('BOARD')
-if not model then
+if not model or model == "" then
 	-- TODO we might ask interactively
 	DIE("Target model has to be provided by BOARD environment variable.")
 end
diff --git a/lists/bootstrap.lua b/lists/bootstrap.lua
index 9fdda058f..bebf98c8d 100644
--- a/lists/bootstrap.lua
+++ b/lists/bootstrap.lua
@@ -1,12 +1,14 @@
 --[[
-Root script for updater-ng configuration usable for bootstrapping.
+Root script for updater-ng configuration used for bootstrapping new root.
 
 This script expects following variables to be possibly defined in environment:
   BOARD: board name (Mox|Omnia|Turris)
   L10N: commas separated list of languages to be installed in root.
   PKGLISTS: commas separated list of package lists to be included in root. To
-	specify options you can optionally add parentheses at the end of package name
-	and list pipe separated options inside them. (ex: foo(opt1|opt2),fee)
+    specify options you can optionally add parentheses at the end of package name
+    and list pipe separated options inside them. (ex: foo(opt1|opt2),fee)
+  CONTRACT: name of contract medkit is generated for. Do not specify for standard
+    medkits
   TESTKEY: if definied non-empty then test kyes are included in installation
 ]]
 
@@ -17,8 +19,11 @@ end
 
 -- Load requested localizations
 l10n = {}
-for lang in os.getenv('L10N'):gmatch('[^,]+') do
-	table.insert(l10n, lang)
+local env_l10n = os.getenv('L10N')
+if env_l10n then
+	for lang in env_l10n:gmatch('[^,]+') do
+		table.insert(l10n, lang)
+	end
 end
 Export('l10n')
 
@@ -34,24 +39,28 @@ Export('for_l10n')
 -- Aways include base script
 Script('base.lua')
 -- Include any additional lists
-for list in os.getenv('PKGLISTS'):gmatch('[^,]+') do
-	local list_name = list:match('^[^(]+') .. ".lua"
-	local list_options = list:match('%((.*)%)$')
-	options = {}
-	Export("options")
-	if list_options then
-		for opt in list_options:gmatch('[^|]+') do
-			options[opt] = true
+local env_pkglists = os.getenv('PKGLISTS')
+if env_pkglists then
+	for list in env_pkglists:gmatch('[^,]+') do
+		local list_name = list:match('^[^(]+') .. ".lua"
+		local list_options = list:match('%((.*)%)$')
+		options = {}
+		Export("options")
+		if list_options then
+			for opt in list_options:gmatch('[^|]+') do
+				options[opt] = true
+			end
 		end
+		Script(list_name .. '.lua')
 	end
-	Script(list_name .. '.lua')
 end
 -- Include contract if specified
-local contract = os.getenv('CONTRACT')
-if contract then
+local env_contract = os.getenv('CONTRACT')
+if env_contract and env_contract ~= "" then
 	Script('contracts/' .. contract .. '.lua')
 end
 
-if os.getenv('TESTKEY') then
+local env_testkey = os.getenv('TESTKEY')
+if env_testkey and env_testkey ~= "" then
 	Install('cznic-repo-keys-test')
 end
-- 
GitLab