Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(just): add brew-command-not-found setup #1874

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions just/bluefin-tools.just
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,60 @@ k8s-dev-tools:
#!/usr/bin/bash
echo "Adding Kubernetes command line tools..."
brew bundle --file /usr/share/ublue-os/homebrew/kubernetes.Brewfile

# Set up command-not-found for Homebrew
setup-brew-not-found ACTION="":
#!/usr/bin/env bash
source /usr/lib/ujust/ujust.sh

OPTION={{ ACTION }}
if [ "$OPTION" == "help" ]; then
echo "Usage: ujust setup-brew-not-found <option>"
echo " <option>: Specify the quick option to skip the prompt"
echo " Use 'enable' to select Enable Brew Not Found"
echo " Use 'disable' to select Disable Brew Not Found"
exit 0
elif [ "$OPTION" == "" ]; then
echo "${bold}Brew command-not-found Setup${normal}"
OPTION=$(Choose "Enable Brew command-not-found" "Disable Brew command-not-found")
fi

set -euo pipefail

BREW_BINARY=/home/linuxbrew/.linuxbrew/bin/brew
if ! $BREW_BINARY -h > /dev/null; then
echo "Make sure Homebrew is first. Check journalctl -e -u brew-setup.service"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure Homebrew is installed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, placed it wrong LOL

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this comment I actually meant that the first sentence in the echo is wrong, isn't it? ;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH! I didnt see that typo till now! Great catch!

exit
fi
BREW_ROOT=${HOMEBREW_REPOSITORY:-$($BREW_BINARY --repository)}
FISH_PATH="/etc/fish/conf.d/brew-command-not-found.fish"
BASH_PATH="/etc/profile.d/brew-command-not-found.sh"

if [[ "${OPTION,,}" =~ ^enable ]]; then
$BREW_BINARY tap homebrew/command-not-found
pkexec tee $BASH_PATH > /dev/null <<EOF
# Check for interactive bash or zsh and that we haven't already been sourced
if [[ -d /home/linuxbrew/.linuxbrew && \$- == *i* && BREW_COMMAND_NOT_FOUND != 1 ]] ; then
HB_CNF_HANDLER="${BREW_ROOT}/Library/Taps/homebrew/homebrew-command-not-found/handler.sh"
Copy link

@lem4s lem4s Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is some magic going on here I don't understand but shouldn't you use the same logic as above: BREW_ROOT=${HOMEBREW_REPOSITORY:-$($BREW_BINARY --repository)} . Otherwise, how do you guarantee that this will work? At least on ghcr.io/ublue-os/bluefin:41-20241110 it seems unset.

Maybe this is the reason why fish is not working for you?
It would be awesome if we can get fish to work as that`s my preferred shell ❤️

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the variable does not have a backslash before it it gets interpreted by the shell, in that code it just expands BREW_ROOT then writes the file, not the other way around

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you are correct, it was quite late yesterday for me ;)
Then I think there could be some issue with hard-coding it here. Suppose the following:

  1. Somebody customized their homebrew installation and BREW_ROOT is set on their system.
  2. He/she then calls this just command. The path gets hard-coded to the customizations. Everything works fine.
  3. Later on the person decides to remove the customizations an remove the brew root under the custom path. This world break brew-command-not-found.

[ -f "\$HB_CNF_HANDLER" ] && source "\$HB_CNF_HANDLER"
export BREW_COMMAND_NOT_FOUND=1
fi
tulilirockz marked this conversation as resolved.
Show resolved Hide resolved
EOF
pkexec tee $FISH_PATH > /dev/null <<EOF
set HB_CNF_HANDLER "${BREW_ROOT}/Library/Taps/homebrew/homebrew-command-not-found/handler.fish"
tulilirockz marked this conversation as resolved.
Show resolved Hide resolved
if test -f \$HB_CNF_HANDLER && test \$BREW_COMMAND_NOT_FOUND != 1
set BREW_COMMAND_NOT_FOUND 1
source \$HB_CNF_HANDLER
end
EOF
echo "Brew command-not-found has been ${b}${green}enabled${n}"
fi

if [[ "${OPTION,,}" =~ ^disable ]]; then
$BREW_BINARY untap homebrew/command-not-found
FILES_TO_BE_REMOVED=()
[ -f $BASH_PATH ] && FILES_TO_BE_REMOVED+=("$BASH_PATH")
[ -f $FISH_PATH ] && FILES_TO_BE_REMOVED+=("$FISH_PATH")
pkexec rm -f "${FILES_TO_BE_REMOVED[@]}"
echo "Brew command-not-found has been ${b}${red}disabled${n}"
fi
Loading