Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tools/modules/functions/set_runtime_variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ function set_runtime_variables() {
DIALOG_CANCEL=1
DIALOG_ESC=255

# use armbian-config configuration file
CONFIG_FILE="/etc/armbian-config"
touch "$CONFIG_FILE"
[[ -f /etc/armbian-config ]] && source "$CONFIG_FILE"

# we have our own lsb_release which does not use Python. Others shell install it here
if [[ ! -f /usr/bin/lsb_release ]]; then
if is_package_manager_running; then
Expand Down Expand Up @@ -113,3 +118,24 @@ function update_kernel_env() {
fi
BRANCH=$new_branch
}

# Function to set or update a variable
#
# Example of usage:
#
# set_config_var "DOMAIN" "www.test.org" "$CONFIG_FILE"
# set_config_var "STORAGE" "/armbian" "$CONFIG_FILE"
#
set_config_var() {
local key="$1"
local value="$2"
local file="$3"

if grep -qE "^${key}=" "$file"; then
# Update existing key
sed -i "s|^${key}=.*|${key}=${value}|" "$file"
else
# Append new key
echo "${key}=${value}" >> "$file"
fi
}
Comment on lines +122 to +141
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix editorconfig failures (tabs vs spaces) and harden set_config_var (escape value; atomic write).

  • CI fails due to indentation with spaces in this block; use tabs to match the file’s style. Also add a final newline at EOF.
  • sed replacement will misbehave if value contains &, |, or backslashes. Escape the replacement string.
  • Use an atomic write (tmp + mv) to reduce the chance of a torn write if multiple processes touch the file.

Apply this diff to address CI and robustness:

-# Function to set or update a variable
-#
-# Example of usage:
-#
-# set_config_var "DOMAIN" "www.test.org" "$CONFIG_FILE"
-# set_config_var "STORAGE" "/armbian" "$CONFIG_FILE"
-#
-set_config_var() {
-    local key="$1"
-    local value="$2"
-    local file="$3"
-
-    if grep -qE "^${key}=" "$file"; then
-        # Update existing key
-        sed -i "s|^${key}=.*|${key}=${value}|" "$file"
-    else
-        # Append new key
-        echo "${key}=${value}" >> "$file"
-    fi
-}
+# Function to set or update a variable
+#
+# Example of usage:
+#
+# set_config_var "DOMAIN" "www.test.org" "$CONFIG_FILE"
+# set_config_var "STORAGE" "/armbian" "$CONFIG_FILE"
+#
+set_config_var() {
+	local key="$1"
+	local value="$2"
+	local file="$3"
+
+	# Escape sed replacement special chars (&, |, \)
+	local esc_value
+	esc_value=$(printf '%s' "$value" | sed -e 's/[\\&|]/\\&/g')
+
+	# Write atomically
+	local tmp
+	tmp="$(mktemp "${file}.XXXXXX")" || return 1
+	if grep -qE "^${key}=" "$file"; then
+		sed "s|^${key}=.*|${key}=${esc_value}|" "$file" > "${tmp}"
+	else
+		cat "$file" > "${tmp}"
+		printf '%s=%s\n' "$key" "$value" >> "${tmp}"
+	fi
+	cat "${tmp}" > "${file}" && rm -f "${tmp}"
+}

Also ensure the file ends with a single trailing newline to satisfy editorconfig.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Function to set or update a variable
#
# Example of usage:
#
# set_config_var "DOMAIN" "www.test.org" "$CONFIG_FILE"
# set_config_var "STORAGE" "/armbian" "$CONFIG_FILE"
#
set_config_var() {
local key="$1"
local value="$2"
local file="$3"
if grep -qE "^${key}=" "$file"; then
# Update existing key
sed -i "s|^${key}=.*|${key}=${value}|" "$file"
else
# Append new key
echo "${key}=${value}" >> "$file"
fi
}
# Function to set or update a variable
#
# Example of usage:
#
# set_config_var "DOMAIN" "www.test.org" "$CONFIG_FILE"
# set_config_var "STORAGE" "/armbian" "$CONFIG_FILE"
#
set_config_var() {
local key="$1"
local value="$2"
local file="$3"
# Escape sed replacement special chars (&, |, \)
local esc_value
esc_value=$(printf '%s' "$value" | sed -e 's/[\\&|]/\\&/g')
# Write atomically
local tmp
tmp="$(mktemp "${file}.XXXXXX")" || return 1
if grep -qE "^${key}=" "$file"; then
sed "s|^${key}=.*|${key}=${esc_value}|" "$file" > "${tmp}"
else
cat "$file" > "${tmp}"
printf '%s=%s\n' "$key" "$value" >> "${tmp}"
fi
cat "${tmp}" > "${file}" && rm -f "${tmp}"
}
🧰 Tools
🪛 GitHub Actions: Coding style check

[error] 130-132: editorconfig-checker: Wrong indentation type(spaces instead of tabs)


[error] 134-140: editorconfig-checker: Wrong indentation type(spaces instead of tabs)

🤖 Prompt for AI Agents
In tools/modules/functions/set_runtime_variables.sh around lines 122-141 the
function uses spaces for indentation (violates editorconfig), performs an unsafe
sed replacement that will break on &, | or backslashes, and writes directly to
the target file (risking torn writes); change indentation in this block to use
tabs and ensure the file ends with a single trailing newline, escape the value
before passing it into sed (or use a sed-safe delimiter and escape &, \ and the
delimiter) so replacements are safe, and perform updates atomically by writing
to a temp file and mv'ing it into place (or use mktemp + trap cleanup) when
updating or appending the key.

6 changes: 4 additions & 2 deletions tools/modules/software/module_swag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ function module_swag() {

case "$1" in
"${commands[0]}")
SWAG_URL=$(dialog --title \
SWAG_URL=$($DIALOG --title \
"Secure Web Application Gateway URL?" \
--inputbox "\nExamples: myhome.domain.org (port 80 and 443 must be exposed to internet)" \
8 80 "" 3>&1 1>&2 2>&3);
8 80 "${DOMAIN}" 3>&1 1>&2 2>&3);

if [[ ${SWAG_URL} && $? -eq 0 ]]; then

Comment on lines +30 to 36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Don’t use $? after command substitution; capture dialog exit status explicitly.

Using $? here tests the assignment, not the dialog’s result. This will treat “Cancel”/ESC as success if the input is non-empty. Capture the rc immediately.

Apply this diff:

-			SWAG_URL=$($DIALOG --title \
+			SWAG_URL=$($DIALOG --title \
 			"Secure Web Application Gateway URL?" \
 			--inputbox "\nExamples: myhome.domain.org (port 80 and 443 must be exposed to internet)" \
-			8 80 "${DOMAIN}" 3>&1 1>&2 2>&3);
-
-			if [[ ${SWAG_URL} && $? -eq 0 ]]; then
+			8 80 "${DOMAIN}" 3>&1 1>&2 2>&3)
+			dialog_rc=$?
+			if [[ ${dialog_rc} -eq 0 && -n "${SWAG_URL}" ]]; then
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
SWAG_URL=$($DIALOG --title \
"Secure Web Application Gateway URL?" \
--inputbox "\nExamples: myhome.domain.org (port 80 and 443 must be exposed to internet)" \
8 80 "" 3>&1 1>&2 2>&3);
8 80 "${DOMAIN}" 3>&1 1>&2 2>&3);
if [[ ${SWAG_URL} && $? -eq 0 ]]; then
SWAG_URL=$($DIALOG --title \
"Secure Web Application Gateway URL?" \
--inputbox "\nExamples: myhome.domain.org (port 80 and 443 must be exposed to internet)" \
8 80 "${DOMAIN}" 3>&1 1>&2 2>&3)
dialog_rc=$?
if [[ ${dialog_rc} -eq 0 && -n "${SWAG_URL}" ]]; then
🤖 Prompt for AI Agents
In tools/modules/software/module_swag.sh around lines 30 to 36, the script
checks $? after doing command substitution which tests the assignment, not the
dialog exit code; change it to capture the dialog exit status immediately into a
variable (e.g. run the dialog into SWAG_URL as currently done, then right after
set rc=$?), and then replace the if condition to test rc -eq 0 and that SWAG_URL
is non-empty (e.g. if [[ $rc -eq 0 && -n "$SWAG_URL" ]]; then). This ensures
Cancel/ESC are detected correctly while preserving the captured input.

# save to configuration file
set_config_var "DOMAIN" "${SWAG_URL}" "$CONFIG_FILE"
# adjust hostname
Comment on lines +37 to 39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Normalize to a bare host before persisting DOMAIN.

Users may enter “https://example.com”, which would store a scheme in DOMAIN. Persist the host only to keep config consistent and to match SWAG’s expected URL semantics.

Apply this diff:

-				# save to configuration file
-				set_config_var "DOMAIN" "${SWAG_URL}" "$CONFIG_FILE"
+				# Normalize and save domain (strip scheme/path/port)
+				DOMAIN_HOST="$(printf '%s' "${SWAG_URL}" | sed -E 's@^[[:space:]]*([A-Za-z][A-Za-z0-9+.-]*://)?([^/:]+).*$@\2@')"
+				# save to configuration file
+				set_config_var "DOMAIN" "${DOMAIN_HOST}" "$CONFIG_FILE"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# save to configuration file
set_config_var "DOMAIN" "${SWAG_URL}" "$CONFIG_FILE"
# adjust hostname
# Normalize and save domain (strip scheme/path/port)
DOMAIN_HOST="$(printf '%s' "${SWAG_URL}" | sed -E 's@^[[:space:]]*([A-Za-z][A-Za-z0-9+.-]*://)?([^/:]+).*$@\2@')"
# save to configuration file
set_config_var "DOMAIN" "${DOMAIN_HOST}" "$CONFIG_FILE"
# adjust hostname
🤖 Prompt for AI Agents
In tools/modules/software/module_swag.sh around lines 37-39, you're currently
persisting the full SWAG_URL into DOMAIN; change this to persist a bare hostname
instead. Extract the host from SWAG_URL by stripping any scheme (http:// or
https://), any path, query or fragment, and any trailing slash (and optionally
strip a port if you want just the hostname), validate the result is non-empty,
then call set_config_var "DOMAIN" with that extracted host and "$CONFIG_FILE"
instead of SWAG_URL so the config stores only the bare host.

hostnamectl set-hostname $(echo ${SWAG_URL} | sed -E 's/^\s*.*:\/\///g')
# install docker
Expand Down