Skip to content

Commit 85c14be

Browse files
authored
Update install.sh (#364)
* Update install.sh * Apply suggestions from code review
1 parent 8140b7e commit 85c14be

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

install.sh

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ get_version() {
4646
fi
4747
}
4848

49+
# Function to check if a directory is writable
50+
is_writable() {
51+
[ -d "$1" ] && [ -w "$1" ]
52+
}
53+
4954
# Function to download and install the CLI tool
5055
install_cli() {
5156
DOWNLOAD_URL="https://github.com/OpsLevel/cli/releases/download/${VERSION}/opslevel-${OS}-${ARCH}.tar.gz"
@@ -64,9 +69,26 @@ install_cli() {
6469
echo "Extracting the OpsLevel CLI..."
6570
tar -xzf "$TMP_DIR/opslevel.tar.gz" -C "$TMP_DIR"
6671

67-
echo "Installing the OpsLevel CLI to /usr/local/bin ..."
68-
sudo mv "$TMP_DIR/opslevel" /usr/local/bin/
6972

73+
74+
# Search for a writable directory in the PATH
75+
TARGET_DIR=""
76+
for dir in $(echo "$PATH" | tr ':' '\n'); do
77+
if is_writable "$dir"; then
78+
TARGET_DIR="$dir"
79+
break
80+
fi
81+
done
82+
83+
# If no writable directory is found, exit
84+
if [ -z "$TARGET_DIR" ]; then
85+
echo "Installation failed. User has no permissions to any directory on PATH"
86+
exit 1
87+
else
88+
echo "Installing the OpsLevel CLI to '$TARGET_DIR' ..."
89+
fi
90+
91+
mv "$TMP_DIR/opslevel" "$TARGET_DIR"
7092
if [ $? -ne 0 ]; then
7193
echo "Installation failed."
7294
exit 1

0 commit comments

Comments
 (0)