From ec4e53330a3c740ec4b8327af501cd9e25668c6a Mon Sep 17 00:00:00 2001 From: gvatsal60 <40350810+gvatsal60@users.noreply.github.com> Date: Sun, 27 Sep 2026 06:59:44 +0000 Subject: [PATCH 1/8] fix(scripts): improve brew execution logic in .update.sh Ensure Homebrew commands are executed correctly by explicitly handling the brew path and dropping privileges to the non-root user. This prevents issues when running under a stripped sudo PATH and ensures brew commands are run in the correct user context with the necessary environment variables loaded. Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com> --- .update.sh | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/.update.sh b/.update.sh index 574b408..4d9bcb4 100755 --- a/.update.sh +++ b/.update.sh @@ -60,6 +60,11 @@ check_cmd() { check_command() { command_name="$1" + # Explicit handling for brew when run under a stripped sudo PATH + if [ "${command_name}" = "brew" ] && [ -x "/home/linuxbrew/.linuxbrew/bin/brew" ]; then + return 0 + fi + if ! command -v "${command_name}" >/dev/null 2>&1; then print_err "${command_name} is not installed." return 1 @@ -220,9 +225,21 @@ update_brew() { return fi - brew update && brew upgrade && brew cleanup -s - println "Brew Diagnostics" - brew doctor && brew missing + # Safely drop privileges back to the normal user to run brew commands + if [ "${NON_ROOT_USER}" != "nobody" ] && [ "${NON_ROOT_USER}" != "root" ]; then + su - "${NON_ROOT_USER}" -c <<'EOF' + eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" + brew update && brew upgrade && brew cleanup -s + echo "\nBrew Diagnostics" + brew doctor && brew missing +EOF + else + # Fallback if no valid non-root user was resolved + eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" + brew update && brew upgrade && brew cleanup -s + println "Brew Diagnostics" + brew doctor && brew missing + fi } # Function: update_vscode_ext From 7d8d7c46471f2425c3b5975b854718d16fc202d9 Mon Sep 17 00:00:00 2001 From: gvatsal60 <40350810+gvatsal60@users.noreply.github.com> Date: Sun, 27 Sep 2026 07:04:11 +0000 Subject: [PATCH 2/8] refactor(scripts): refactor brew execution logic in .update.sh Consolidate brew commands into a single variable to ensure consistent execution between the non-root user context and the fallback path. This reduces duplication and improves maintainability of the update logic. Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com> --- .update.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.update.sh b/.update.sh index 4d9bcb4..c79a853 100755 --- a/.update.sh +++ b/.update.sh @@ -225,20 +225,20 @@ update_brew() { return fi + # Common brew commands to execute (escape $ to prevent early expansion) + _brew_cmds=" + eval \"\$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)\" + brew update && brew upgrade && brew cleanup -s + echo \"\\nBrew Diagnostics\" + brew doctor && brew missing + " + # Safely drop privileges back to the normal user to run brew commands if [ "${NON_ROOT_USER}" != "nobody" ] && [ "${NON_ROOT_USER}" != "root" ]; then - su - "${NON_ROOT_USER}" -c <<'EOF' - eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" - brew update && brew upgrade && brew cleanup -s - echo "\nBrew Diagnostics" - brew doctor && brew missing -EOF + su - "${NON_ROOT_USER}" -c "${_brew_cmds}" else # Fallback if no valid non-root user was resolved - eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" - brew update && brew upgrade && brew cleanup -s - println "Brew Diagnostics" - brew doctor && brew missing + eval "${_brew_cmds}" fi } From d5cb03935b879cea5a32fff2b81411f1df7b369e Mon Sep 17 00:00:00 2001 From: gvatsal60 <40350810+gvatsal60@users.noreply.github.com> Date: Sun, 27 Sep 2026 07:16:58 +0000 Subject: [PATCH 3/8] fix(brew): use dynamic brew path to match check logic Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com> --- .update.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.update.sh b/.update.sh index c79a853..cc0e077 100755 --- a/.update.sh +++ b/.update.sh @@ -225,9 +225,15 @@ update_brew() { return fi + # Detect brew path dynamically to match check_command logic + _brew_path=$(command -v brew 2>/dev/null) + if [ -z "${_brew_path}" ] && [ -x "/home/linuxbrew/.linuxbrew/bin/brew" ]; then + _brew_path="/home/linuxbrew/.linuxbrew/bin/brew" + fi + # Common brew commands to execute (escape $ to prevent early expansion) _brew_cmds=" - eval \"\$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)\" + eval \"\$(${_brew_path} shellenv)\" brew update && brew upgrade && brew cleanup -s echo \"\\nBrew Diagnostics\" brew doctor && brew missing From a06e6a9fb68d527ca881e5870c824de702fccd52 Mon Sep 17 00:00:00 2001 From: gvatsal60 <40350810+gvatsal60@users.noreply.github.com> Date: Sun, 27 Sep 2026 07:17:37 +0000 Subject: [PATCH 4/8] style(brew): remove leading newline in _brew_cmds variable Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com> --- .update.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.update.sh b/.update.sh index cc0e077..50debd9 100755 --- a/.update.sh +++ b/.update.sh @@ -232,12 +232,10 @@ update_brew() { fi # Common brew commands to execute (escape $ to prevent early expansion) - _brew_cmds=" - eval \"\$(${_brew_path} shellenv)\" + _brew_cmds="eval \"\$(${_brew_path} shellenv)\" brew update && brew upgrade && brew cleanup -s echo \"\\nBrew Diagnostics\" - brew doctor && brew missing - " + brew doctor && brew missing" # Safely drop privileges back to the normal user to run brew commands if [ "${NON_ROOT_USER}" != "nobody" ] && [ "${NON_ROOT_USER}" != "root" ]; then From 58abfc1e6b628c1d7f10d2ae9bead635fc2a6d95 Mon Sep 17 00:00:00 2001 From: gvatsal60 <40350810+gvatsal60@users.noreply.github.com> Date: Sun, 27 Sep 2026 07:17:59 +0000 Subject: [PATCH 5/8] style(brew): use printf for portable newline handling Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com> --- .update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.update.sh b/.update.sh index 50debd9..15d31ec 100755 --- a/.update.sh +++ b/.update.sh @@ -234,7 +234,7 @@ update_brew() { # Common brew commands to execute (escape $ to prevent early expansion) _brew_cmds="eval \"\$(${_brew_path} shellenv)\" brew update && brew upgrade && brew cleanup -s - echo \"\\nBrew Diagnostics\" + printf \"\\nBrew Diagnostics\\n\" brew doctor && brew missing" # Safely drop privileges back to the normal user to run brew commands From 4f1b7265b8ffb325725483600cd7a8dae14d10df Mon Sep 17 00:00:00 2001 From: gvatsal60 <40350810+gvatsal60@users.noreply.github.com> Date: Sun, 27 Sep 2026 07:18:28 +0000 Subject: [PATCH 6/8] fix(brew): add error handling for brew command execution Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com> --- .update.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.update.sh b/.update.sh index 15d31ec..163850c 100755 --- a/.update.sh +++ b/.update.sh @@ -239,10 +239,16 @@ update_brew() { # Safely drop privileges back to the normal user to run brew commands if [ "${NON_ROOT_USER}" != "nobody" ] && [ "${NON_ROOT_USER}" != "root" ]; then - su - "${NON_ROOT_USER}" -c "${_brew_cmds}" + if ! su - "${NON_ROOT_USER}" -c "${_brew_cmds}"; then + print_err "Error: Brew commands failed for user ${NON_ROOT_USER}." + return 1 + fi else # Fallback if no valid non-root user was resolved - eval "${_brew_cmds}" + if ! eval "${_brew_cmds}"; then + print_err "Error: Brew commands failed." + return 1 + fi fi } From 152f4e0ebc93f16bbb78ab6c04b32eb70a760a69 Mon Sep 17 00:00:00 2001 From: gvatsal60 <40350810+gvatsal60@users.noreply.github.com> Date: Sun, 27 Sep 2026 07:18:54 +0000 Subject: [PATCH 7/8] fix(brew): force POSIX shell for su command Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com> --- .update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.update.sh b/.update.sh index 163850c..32d63e7 100755 --- a/.update.sh +++ b/.update.sh @@ -239,7 +239,7 @@ update_brew() { # Safely drop privileges back to the normal user to run brew commands if [ "${NON_ROOT_USER}" != "nobody" ] && [ "${NON_ROOT_USER}" != "root" ]; then - if ! su - "${NON_ROOT_USER}" -c "${_brew_cmds}"; then + if ! su - "${NON_ROOT_USER}" -s /bin/sh -c "${_brew_cmds}"; then print_err "Error: Brew commands failed for user ${NON_ROOT_USER}." return 1 fi From e730f9477b3531edb2512f035dc169a29b6ba5b7 Mon Sep 17 00:00:00 2001 From: Vatsal Gupta <40350810+gvatsal60@users.noreply.github.com> Date: Sun, 27 Sep 2026 08:57:17 +0000 Subject: [PATCH 8/8] Updated the script --- .update.sh | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/.update.sh b/.update.sh index 32d63e7..e768e28 100755 --- a/.update.sh +++ b/.update.sh @@ -40,19 +40,6 @@ print_err() { printf "\n${RED}%s${CLEAR}\n" "$*" >&2 } -# Function: check_cmd -# Description: Checks if a specified command is available in the system. -# Usage: check_cmd "command_name" -check_cmd() { - command_name="$1" - - if ! command -v "${command_name}" >/dev/null 2>&1; then - return 1 - fi - - return 0 -} - # Function: check_command # Description: Checks if a specified command is available in the system. # Prints a message indicating whether the command is installed. @@ -60,11 +47,6 @@ check_cmd() { check_command() { command_name="$1" - # Explicit handling for brew when run under a stripped sudo PATH - if [ "${command_name}" = "brew" ] && [ -x "/home/linuxbrew/.linuxbrew/bin/brew" ]; then - return 0 - fi - if ! command -v "${command_name}" >/dev/null 2>&1; then print_err "${command_name} is not installed." return 1 @@ -221,14 +203,16 @@ update_os_pkg() { update_brew() { println "Update Brew Formula's" - if ! check_command brew; then - return - fi - # Detect brew path dynamically to match check_command logic _brew_path=$(command -v brew 2>/dev/null) - if [ -z "${_brew_path}" ] && [ -x "/home/linuxbrew/.linuxbrew/bin/brew" ]; then - _brew_path="/home/linuxbrew/.linuxbrew/bin/brew" + + if [ -z "${_brew_path}" ]; then + if [ -x "/home/linuxbrew/.linuxbrew/bin/brew" ]; then + _brew_path="/home/linuxbrew/.linuxbrew/bin/brew" + else + print_err "brew is not installed." + return + fi fi # Common brew commands to execute (escape $ to prevent early expansion) @@ -241,13 +225,13 @@ update_brew() { if [ "${NON_ROOT_USER}" != "nobody" ] && [ "${NON_ROOT_USER}" != "root" ]; then if ! su - "${NON_ROOT_USER}" -s /bin/sh -c "${_brew_cmds}"; then print_err "Error: Brew commands failed for user ${NON_ROOT_USER}." - return 1 + return fi else # Fallback if no valid non-root user was resolved if ! eval "${_brew_cmds}"; then print_err "Error: Brew commands failed." - return 1 + return fi fi }