Skip to content

Commit 3a55db0

Browse files
Handle passwordless sudo in install script (#9065)
* Handle passwordless sudo in install script * nits
1 parent 0111b56 commit 3a55db0

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

scripts/install.sh

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ checkConflictingInstallation() {
131131
fi
132132
}
133133

134-
# Install the binary and ask for elevated permissions if needed
134+
# Install the binary, using sudo if the directory is not directly writable
135135
installBinary() {
136-
if [ "$INSTALL_DIR" = "/usr/local/bin" ]; then
136+
if { [ -d "$INSTALL_DIR" ] && [ -w "$INSTALL_DIR" ]; } || { [ ! -d "$INSTALL_DIR" ] && [ -w "$(dirname "$INSTALL_DIR")" ]; }; then
137+
install -d "$INSTALL_DIR"
138+
install rill "$INSTALL_DIR"
139+
else
137140
printf "\nElevated permissions required to install the Rill binary to: %s/rill\n" "$INSTALL_DIR"
138141
sudo install -d "$INSTALL_DIR"
139142
sudo install rill "$INSTALL_DIR"
140-
else
141-
install -d "$INSTALL_DIR"
142-
install rill "$INSTALL_DIR"
143143
fi
144144
cd - > /dev/null
145145
}
@@ -225,13 +225,18 @@ removePathConfigEntries() {
225225
done
226226
}
227227

228-
# Check if the install directory (or its parent, if it doesn't exist yet) is writable
228+
# Check if we can install to INSTALL_DIR
229229
installDirIsWritable() {
230-
if [ -d "$INSTALL_DIR" ]; then
231-
[ -w "$INSTALL_DIR" ]
232-
else
233-
[ -w "$(dirname "$INSTALL_DIR")" ]
230+
# Check if it is directly writable
231+
if [ -d "$INSTALL_DIR" ] && [ -w "$INSTALL_DIR" ]; then
232+
return 0
233+
# If it doesn't exist yet, check if the parent directory is writable
234+
elif [ ! -d "$INSTALL_DIR" ] && [ -w "$(dirname "$INSTALL_DIR")" ]; then
235+
return 0
234236
fi
237+
238+
# Lastly, we also consider it writable if we have passwordless sudo (since then we don't need to prompt/error due to lack of permissions)
239+
sudo -n true 2>/dev/null
235240
}
236241

237242
# Resolve the install directory
@@ -248,18 +253,20 @@ resolveInstallDir() {
248253

249254
# Handle non-interactive scenarios where prompt or sudo are not possible
250255
if [ "$NON_INTERACTIVE" = "true" ]; then
251-
# If the install directory was explicitly set and requires sudo, we error
252-
if [ -n "$INSTALL_DIR" ] && [ "$INSTALL_DIR_EXPLICIT" = "true" ] && ! installDirIsWritable; then
253-
printf "Install directory '%s' requires elevated permissions, which are not available in non-interactive mode.\n" "$INSTALL_DIR"
254-
exit 1
256+
# Default to /usr/local/bin if not set.
257+
if [ -z "$INSTALL_DIR" ]; then
258+
INSTALL_DIR="/usr/local/bin"
255259
fi
256260

257-
# If the install directory is not set, or the previous installation path requires sudo, we default to installing in the current directory
258-
if [ -z "$INSTALL_DIR" ]; then
259-
printf "Non-interactive shell detected; defaulting to install in current directory.\n"
260-
INSTALL_DIR=$(pwd)
261-
elif ! installDirIsWritable; then
262-
printf "Non-interactive shell detected; previous installation at '%s' requires elevated permissions; defaulting to install in current directory.\n" "$INSTALL_DIR"
261+
# Handle if the install directory is not writable and we can't prompt due to non-interactive mode.
262+
if ! installDirIsWritable; then
263+
# Error if the install directory was set explicitly.
264+
if [ "$INSTALL_DIR_EXPLICIT" = "true" ]; then
265+
printf "Install directory '%s' requires elevated permissions, which are not available in non-interactive mode.\n" "$INSTALL_DIR"
266+
exit 1
267+
fi
268+
269+
# Fall back to the current directory otherwise (which we assume is writable).
263270
INSTALL_DIR=$(pwd)
264271
fi
265272

0 commit comments

Comments
 (0)