Skip to content

Commit

Permalink
fix(cli): change default installation directory (#873)
Browse files Browse the repository at this point in the history
<!--
Pull requests are squashed and merged using:
- their title as the commit message
- their description as the commit body

Having a good title and description is important for the users to get
readable changelog.
-->

<!-- 1. Explain WHAT the change is about -->

- Remplacement PR for #843.

<!-- 2. Explain WHY the change cannot be made simpler -->

- ...

<!-- 3. Explain HOW users should update their code -->

#### Migration notes

...

- [ ] The change comes with new or modified tests
- [ ] Hard-to-understand functions have explanatory comments
- [ ] End-user documentation is updated to reflect the change

---------

Co-authored-by: Natoandro <[email protected]>
  • Loading branch information
luckasRanarison and Natoandro authored Oct 16, 2024
1 parent a3310f4 commit 719445f
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LATEST_VERSION="${LATEST_VERSION##*v}"

PLATFORM="${PLATFORM:-}"
TMP_DIR=$(mktemp -d)
OUT_DIR="${OUT_DIR:-/usr/local/bin}"
OUT_DIR="${OUT_DIR:-$HOME/.metatype/bin}"
VERSION="${VERSION:-$LATEST_VERSION}"
MACHINE=$(uname -m)

Expand Down Expand Up @@ -102,24 +102,59 @@ Set the OUT_DIR environment variable to change the installation directory:
$ curl -fsSL $INSTALLER_URL | OUT_DIR=. bash
EOF
if [ ! -d "${OUT_DIR}" ]; then
mkdir -p "$OUT_DIR"
fi

if [ -w "${OUT_DIR}" ]; then
read -p "Press enter to continue (or cancel with Ctrl+C):"
mv "$TMP_DIR/$EXE" "$OUT_DIR"
else
printf "Sudo is required to run \"sudo mv %s %s\":\n" "$TMP_DIR/$EXE" "$OUT_DIR"
sudo mv "$TMP_DIR/$EXE" "$OUT_DIR"
echo "$OUT_DIR is not writable."
exit 1
fi
fi

rm -r "$TMP_DIR"

OUT_DIR=$(realpath $OUT_DIR)
if [[ ":$PATH:" != *":$OUT_DIR:"* ]]; then
cat <<EOF
SHELL_TYPE=$(basename "$SHELL")

The installation directory is not in your PATH, consider adding it:
$ export PATH="\$PATH:$OUT_DIR"
Or moving the executable to another directory in your PATH:
$ sudo mv $EXE /usr/local/bin
case $SHELL_TYPE in
bash|zsh|ksh)
SHELL_CONFIG="$HOME/.$SHELL_TYPE"rc
;;
fish)
SHELL_CONFIG="$HOME/.config/fish/config.fish"
;;
*)
SHELL_CONFIG=""
esac

if [ -n "$SHELL_CONFIG" ]; then
printf "\nDetected shell: %s\n" "$SHELL_TYPE"
read -p "Do you want to append the new PATH to your configuration ($SHELL_CONFIG)? (y/n): " answer

answer=$(echo "$answer" | tr "[:upper:]" "[:lower:]")

case $SHELL_TYPE in
bash|zsh|ksh)
APPEND_CMD="export PATH=\"$OUT_DIR:\$PATH\""
;;
fish)
APPEND_CMD="fish_add_path $OUT_DIR"
;;
esac

if [ "$answer" = "y" ] || [ "$answer" = "yes" ]; then
echo "$APPEND_CMD" >> "$SHELL_CONFIG"
printf "Path added to %s\nRun 'source %s' to apply changes." "$SHELL_CONFIG" "$SHELL_CONFIG"
else
cat <<EOF
Consider adding $OUT_DIR to your PATH if it is not already configured.
$ $APPEND_CMD
EOF
fi
else
printf "\nConsider adding %s to your PATH if it is not already configured." "$OUT_DIR"
fi

0 comments on commit 719445f

Please sign in to comment.