Skip to content

Commit

Permalink
fix: check writability of target path when deciding to use sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
kldzj committed May 9, 2023
1 parent 02c78e4 commit 6d643d4
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,64 @@ get_arch() {
}

use_sudo() {
if command -v sudo >/dev/null 2>&1 && [ "$(id -u)" -ne 0 ]; then
echo "yes"
local target="$1"
local sudo="no"

if ! command -v sudo >/dev/null; then
echo "$sudo"
return
fi

if [ -w "$(dirname "$target")" ]; then
if [ -f "$target" ] && [ ! -w "$target" ]; then
sudo="yes"
fi
else
sudo="yes"
fi

echo "$sudo"
}

pzmod_in_path() {
local filename="$1"
local in_path="no"

if command -v "$filename" >/dev/null; then
in_path="yes"
fi

echo "$in_path"
}

finalize_installation() {
local sudo_needed="$1"
local target="${2:-/usr/local/bin/pzmod}"
local filename="$(basename "$target")"
local in_path=$(pzmod_in_path "$filename")

if [ "$sudo_needed" = "yes" ]; then
sudo chmod +x "$target"
else
echo "no"
chmod +x "$target"
fi

if [ "$in_path" = "no" ]; then
echo "Warning: $filename not found in PATH."
echo "You may want to add the directory to your PATH environment variable,"
echo "or move the executable to a directory that's already in your PATH."
echo
fi

echo "pzmod successfully installed to $target"
}

download_pzmod() {
local latest_url="https://api.github.com/repos/kldzj/pzmod/releases/latest"
local platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
local arch="$(get_arch)"
local target="${1:-/usr/local/bin/pzmod}"
local sudo_needed=$(use_sudo)
local sudo_needed=$(use_sudo "$target")

local download_url=$(curl -s "$latest_url" |
grep "browser_download_url" |
Expand All @@ -50,12 +95,10 @@ download_pzmod() {

if [ "$sudo_needed" = "yes" ]; then
sudo wget -O "$target" "$download_url" &&
sudo chmod +x "$target" &&
echo "pzmod successfully downloaded to $target"
finalize_installation "$sudo_needed" "$target"
else
wget -O "$target" "$download_url" &&
chmod +x "$target" &&
echo "pzmod successfully downloaded to $target"
finalize_installation "$sudo_needed" "$target"
fi
}

Expand Down

0 comments on commit 6d643d4

Please sign in to comment.