Skip to content

Commit 6d643d4

Browse files
committed
fix: check writability of target path when deciding to use sudo
1 parent 02c78e4 commit 6d643d4

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

install.sh

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,64 @@ get_arch() {
2424
}
2525

2626
use_sudo() {
27-
if command -v sudo >/dev/null 2>&1 && [ "$(id -u)" -ne 0 ]; then
28-
echo "yes"
27+
local target="$1"
28+
local sudo="no"
29+
30+
if ! command -v sudo >/dev/null; then
31+
echo "$sudo"
32+
return
33+
fi
34+
35+
if [ -w "$(dirname "$target")" ]; then
36+
if [ -f "$target" ] && [ ! -w "$target" ]; then
37+
sudo="yes"
38+
fi
39+
else
40+
sudo="yes"
41+
fi
42+
43+
echo "$sudo"
44+
}
45+
46+
pzmod_in_path() {
47+
local filename="$1"
48+
local in_path="no"
49+
50+
if command -v "$filename" >/dev/null; then
51+
in_path="yes"
52+
fi
53+
54+
echo "$in_path"
55+
}
56+
57+
finalize_installation() {
58+
local sudo_needed="$1"
59+
local target="${2:-/usr/local/bin/pzmod}"
60+
local filename="$(basename "$target")"
61+
local in_path=$(pzmod_in_path "$filename")
62+
63+
if [ "$sudo_needed" = "yes" ]; then
64+
sudo chmod +x "$target"
2965
else
30-
echo "no"
66+
chmod +x "$target"
67+
fi
68+
69+
if [ "$in_path" = "no" ]; then
70+
echo "Warning: $filename not found in PATH."
71+
echo "You may want to add the directory to your PATH environment variable,"
72+
echo "or move the executable to a directory that's already in your PATH."
73+
echo
3174
fi
75+
76+
echo "pzmod successfully installed to $target"
3277
}
3378

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

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

5196
if [ "$sudo_needed" = "yes" ]; then
5297
sudo wget -O "$target" "$download_url" &&
53-
sudo chmod +x "$target" &&
54-
echo "pzmod successfully downloaded to $target"
98+
finalize_installation "$sudo_needed" "$target"
5599
else
56100
wget -O "$target" "$download_url" &&
57-
chmod +x "$target" &&
58-
echo "pzmod successfully downloaded to $target"
101+
finalize_installation "$sudo_needed" "$target"
59102
fi
60103
}
61104

0 commit comments

Comments
 (0)