Skip to content

Commit

Permalink
Merge pull request #263 from DopplerHQ/print-install-perms-error
Browse files Browse the repository at this point in the history
Print error if install.sh fails due to lack of write perms
  • Loading branch information
Piccirello authored Oct 5, 2021
2 parents 043e36f + 62c8d01 commit 147ca1b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
11 changes: 8 additions & 3 deletions pkg/controllers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ func RunInstallScript() (bool, string, Error) {
fmt.Println(strOut)
}
if err != nil {
exitCode := 1
if exitError, ok := err.(*exec.ExitError); ok {
exitCode = exitError.ExitCode()
}

message := "Unable to install the latest Doppler CLI"
// check for errors indicating lack of perms
if strings.Contains(strOut, "dpkg: error: requested operation requires superuser privilege") {
message = "Error: update failed due to improper permissions\nPlease re-run with `sudo` or run as the root user"
permissionError := exitCode == 2 || strings.Contains(strOut, "dpkg: error: requested operation requires superuser privilege")
if permissionError {
message = "Error: update failed due to improper permissions\nPlease re-run with `sudo` or as an admin"
}

return false, "", Error{Err: err, Message: message}
Expand Down
48 changes: 37 additions & 11 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

set -e

# error codes
# 1 general
# 2 insufficient perms

DEBUG=0
INSTALL=1
CLEAN_EXIT=0
Expand Down Expand Up @@ -218,6 +222,13 @@ is_dir_in_path() {
echo "$found"
}

is_path_writable() {
dir="$1"
writable=1
test -w "$dir" || writable=0
echo "$writable"
}

# flag parsing
for arg; do
if [ "$arg" = "--debug" ]; then
Expand Down Expand Up @@ -447,32 +458,47 @@ elif [ "$format" = "tar" ]; then
# install
if [ "$INSTALL" -eq 1 ]; then
echo 'Installing...'
found_valid_path=0
binary_installed=0

install_dir="/usr/local/bin"
if [ -d "$install_dir" ] && [ "$( is_dir_in_path "$install_dir" )" -eq "1" ]; then
log_debug "Moving binary to $install_dir"
mv -f "$extract_dir/doppler" $install_dir
binary_installed=1
found_valid_path=1
if [ "$( is_path_writable "$install_dir" )" -eq "1" ]; then
log_debug "Moving binary to $install_dir"
mv -f "$extract_dir/doppler" $install_dir
binary_installed=1
fi
fi

install_dir="/usr/bin"
if [ "$binary_installed" -eq 0 ] && [ -d "$install_dir" ] && [ "$( is_dir_in_path "$install_dir" )" -eq "1" ]; then
log_debug "Moving binary to $install_dir"
mv -f "$extract_dir/doppler" $install_dir
binary_installed=1
found_valid_path=1
if [ "$( is_path_writable "$install_dir" )" -eq "1" ]; then
log_debug "Moving binary to $install_dir"
mv -f "$extract_dir/doppler" $install_dir
binary_installed=1
fi
fi

install_dir="/usr/sbin"
if [ "$binary_installed" -eq 0 ] && [ -d "$install_dir" ] && [ "$( is_dir_in_path "$install_dir" )" -eq "1" ]; then
log_debug "Moving binary to $install_dir"
mv -f "$extract_dir/doppler" $install_dir
binary_installed=1
found_valid_path=1
if [ "$( is_path_writable "$install_dir" )" -eq "1" ]; then
log_debug "Moving binary to $install_dir"
mv -f "$extract_dir/doppler" $install_dir
binary_installed=1
fi
fi

if [ "$binary_installed" -eq 0 ]; then
log "No supported bin directories are available; please adjust your PATH"
clean_exit 1
if [ "$found_valid_path" -eq 0 ]; then
log "No supported bin directories are available; please adjust your PATH"
clean_exit 1
else
log "Unable to write to bin directory; please re-run with \`sudo\` or as an admin"
clean_exit 2
fi
fi
else
log_debug "Moving binary to $(pwd) (cwd)"
Expand Down

0 comments on commit 147ca1b

Please sign in to comment.