@@ -23,6 +23,16 @@ require_cmd() {
2323 command -v " $1 " > /dev/null 2>&1
2424}
2525
26+ # Run a command with elevation only when needed (non-root). Root can omit sudo.
27+ run_as_root () {
28+ if [ " $( id -u) " -eq 0 ]; then
29+ " $@ "
30+ else
31+ require_cmd sudo || error " sudo is required to install system packages (install sudo or run this script as root)."
32+ sudo " $@ "
33+ fi
34+ }
35+
2636ensure_nvm_loaded () {
2737 export NVM_DIR=" ${NVM_DIR:- $HOME / .nvm} "
2838 if [ -s " $NVM_DIR /nvm.sh" ]; then
@@ -89,54 +99,48 @@ install_gh_macos() {
8999
90100install_gh_linux_apt () {
91101 info " Installing GitHub CLI with apt (Debian/Ubuntu)."
92- require_cmd sudo || error " sudo is required to install packages with apt."
93- sudo apt-get update -y || error " apt-get update failed."
102+ run_as_root apt-get update -y || error " apt-get update failed."
94103 if ! require_cmd curl; then
95- sudo apt-get install -y curl || error " Failed to install curl (needed for GitHub CLI apt setup)."
104+ run_as_root apt-get install -y curl || error " Failed to install curl (needed for GitHub CLI apt setup)."
96105 fi
97- sudo install -d -m 755 /etc/apt/keyrings
106+ run_as_root install -d -m 755 /etc/apt/keyrings
98107 curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg |
99- sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null || error " Failed to add GitHub CLI apt key."
100- sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
108+ run_as_root tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null || error " Failed to add GitHub CLI apt key."
109+ run_as_root chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
101110 echo " deb [arch=$( dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" |
102- sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null || error " Failed to add GitHub CLI apt source."
103- sudo apt-get update -y || error " apt-get update failed after adding GitHub CLI source."
104- sudo apt-get install -y gh || error " apt failed to install gh."
111+ run_as_root tee /etc/apt/sources.list.d/github-cli.list > /dev/null || error " Failed to add GitHub CLI apt source."
112+ run_as_root apt-get update -y || error " apt-get update failed after adding GitHub CLI source."
113+ run_as_root apt-get install -y gh || error " apt failed to install gh."
105114}
106115
107116install_gh_linux_dnf () {
108117 info " Installing GitHub CLI with dnf (Fedora/RHEL-compatible)."
109- require_cmd sudo || error " sudo is required to install packages with dnf."
110- sudo dnf install -y ' dnf-command(config-manager)' || warn " dnf-command(config-manager) may already be installed; continuing."
111- sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || error " Failed to add gh dnf repository."
112- sudo dnf install -y gh || error " dnf failed to install gh."
118+ run_as_root dnf install -y ' dnf-command(config-manager)' || warn " dnf-command(config-manager) may already be installed; continuing."
119+ run_as_root dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || error " Failed to add gh dnf repository."
120+ run_as_root dnf install -y gh || error " dnf failed to install gh."
113121}
114122
115123install_gh_linux_yum () {
116124 info " Installing GitHub CLI with yum (older RHEL/CentOS)."
117- require_cmd sudo || error " sudo is required to install packages with yum."
118- sudo yum install -y yum-utils || error " yum-utils installation failed."
119- sudo yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || error " Failed to add gh yum repository."
120- sudo yum install -y gh || error " yum failed to install gh."
125+ run_as_root yum install -y yum-utils || error " yum-utils installation failed."
126+ run_as_root yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || error " Failed to add gh yum repository."
127+ run_as_root yum install -y gh || error " yum failed to install gh."
121128}
122129
123130install_gh_linux_pacman () {
124131 info " Installing GitHub CLI with pacman (Arch)."
125- require_cmd sudo || error " sudo is required to install packages with pacman."
126- sudo pacman -Sy --noconfirm github-cli || error " pacman failed to install github-cli."
132+ run_as_root pacman -Syu --noconfirm github-cli || error " pacman failed to install github-cli."
127133}
128134
129135install_gh_linux_zypper () {
130136 info " Installing GitHub CLI with zypper (openSUSE)."
131- require_cmd sudo || error " sudo is required to install packages with zypper."
132- sudo zypper refresh
133- sudo zypper install -y gh || error " zypper failed to install gh."
137+ run_as_root zypper refresh
138+ run_as_root zypper install -y gh || error " zypper failed to install gh."
134139}
135140
136141install_gh_linux_apk () {
137142 info " Installing GitHub CLI with apk (Alpine)."
138- require_cmd sudo || error " sudo is required to install packages with apk."
139- sudo apk add --no-cache github-cli || error " apk failed to install github-cli."
143+ run_as_root apk add --no-cache github-cli || error " apk failed to install github-cli."
140144}
141145
142146install_gh_linux () {
0 commit comments