Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
feat: prompt integration with quadlets (ublue-os#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
m2Giles authored Feb 5, 2024
1 parent f75119d commit 61ea499
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 26 deletions.
6 changes: 6 additions & 0 deletions fetch-quadlets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,42 @@ mkdir -p /usr/etc/containers/systemd/users

# bluefin-cli
wget --output-document="/usr/etc/containers/systemd/users/bluefin-cli.container" --quiet https://raw.githubusercontent.com/ublue-os/toolboxes/main/quadlets/bluefin-cli/bluefin-cli.container
cat /usr/share/ublue-os/bluefin-cli/prompt-integration >> /usr/etc/containers/systemd/users/bluefin-cli.container
printf "\n\n[Install]\nWantedBy=bluefin-cli.target" >> /usr/etc/containers/systemd/users/bluefin-cli.container
sed -i '/AutoUpdate.*/ s/^#*/#/' /usr/etc/containers/systemd/users/bluefin-cli.container
sed -i 's/ContainerName=bluefin/ContainerName=bluefin-cli/' /usr/etc/containers/systemd/users/bluefin-cli.container

# bluefin-dx-cli
wget --output-document="/usr/etc/containers/systemd/users/bluefin-dx-cli.container" --quiet https://raw.githubusercontent.com/ublue-os/toolboxes/main/quadlets/bluefin-cli/bluefin-dx-cli.container
cat /usr/share/ublue-os/bluefin-cli/prompt-integration >> /usr/etc/containers/systemd/users/bluefin-dx-cli.container
printf "\n\n[Install]\nWantedBy=bluefin-dx-cli.target" >> /usr/etc/containers/systemd/users/bluefin-dx-cli.container
sed -i '/AutoUpdate.*/ s/^#*/#/' /usr/etc/containers/systemd/users/bluefin-dx-cli.container
sed -i 's/ContainerName=bluefin/ContainerName=bluefin-dx-cli/' /usr/etc/containers/systemd/users/bluefin-dx-cli.container

# fedora-toolbox
wget --output-document="/usr/etc/containers/systemd/users/fedora-toolbox.container" --quiet https://raw.githubusercontent.com/ublue-os/toolboxes/main/quadlets/fedora-toolbox/fedora-distrobox-quadlet.container
cat /usr/share/ublue-os/bluefin-cli/prompt-integration >> /usr/etc/containers/systemd/users/fedora-toolbox.container
printf "\n\n[Install]\nWantedBy=fedora-toolbox.target" >> /usr/etc/containers/systemd/users/fedora-toolbox.container
sed -i '/AutoUpdate.*/ s/^#*/#/' /usr/etc/containers/systemd/users/fedora-toolbox.container
sed -i 's/ContainerName=fedora-distrobox-quadlet/ContainerName=fedora-toolbox/' /usr/etc/containers/systemd/users/fedora-toolbox.container

# ubuntu-toolbox
wget --output-document="/usr/etc/containers/systemd/users/ubuntu-toolbox.container" --quiet https://raw.githubusercontent.com/ublue-os/toolboxes/main/quadlets/ubuntu-toolbox/ubuntu-distrobox-quadlet.container
cat /usr/share/ublue-os/bluefin-cli/prompt-integration >> /usr/etc/containers/systemd/users/ubuntu-toolbox.container
printf "\n\n[Install]\nWantedBy=ubuntu-toolbox.target" >> /usr/etc/containers/systemd/users/ubuntu-toolbox.container
sed -i '/AutoUpdate.*/ s/^#*/#/' /usr/etc/containers/systemd/users/ubuntu-toolbox.container
sed -i 's/ContainerName=ubuntu-distrobox-quadlet/ContainerName=ubuntu-toolbox/' /usr/etc/containers/systemd/users/ubuntu-toolbox.container

# wolfi-toolbox
wget --output-document="/usr/etc/containers/systemd/users/wolfi-toolbox.container" --quiet https://raw.githubusercontent.com/ublue-os/toolboxes/main/quadlets/wolfi-toolbox/wolfi-distrobox-quadlet.container
cat /usr/share/ublue-os/bluefin-cli/prompt-integration >> /usr/etc/containers/systemd/users/wolfi-toolbox.container
printf "\n\n[Install]\nWantedBy=wolfi-toolbox.target" >> /usr/etc/containers/systemd/users/wolfi-toolbox.container
sed -i '/AutoUpdate.*/ s/^#*/#/' /usr/etc/containers/systemd/users/wolfi-toolbox.container
sed -i 's/ContainerName=wolfi-quadlet/ContainerName=wolfi-toolbox/' /usr/etc/containers/systemd/users/wolfi-toolbox.container

# wolfi-dx-toolbox
wget --output-document="/usr/etc/containers/systemd/users/wolfi-dx-toolbox.container" --quiet https://raw.githubusercontent.com/ublue-os/toolboxes/main/quadlets/wolfi-toolbox/wolfi-dx-distrobox-quadlet.container
cat /usr/share/ublue-os/bluefin-cli/prompt-integration >> /usr/etc/containers/systemd/users/wolfi-dx-toolbox.container
printf "\n\n[Install]\nWantedBy=wolfi-dx-toolbox.target" >> /usr/etc/containers/systemd/users/wolfi-dx-toolbox.container
sed -i '/AutoUpdate.*/ s/^#*/#/' /usr/etc/containers/systemd/users/wolfi-dx-toolbox.container
sed -i 's/ContainerName=wolfi-quadlet/ContainerName=wolfi-dx-toolbox/' /usr/etc/containers/systemd/users/wolfi-dx-toolbox.container
Expand Down
9 changes: 7 additions & 2 deletions just/custom.just
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ benchmark:
echo 'Running a 1 minute benchmark ...'
stress-ng --matrix 0 -t 1m --times

# Configure bluefin-cli Terminal Experience
# Configure Bluefin-CLI Terminal Experience
bluefin-cli:
/usr/libexec/bluefin-cli.sh
@/usr/libexec/enable-bluefin-cli.sh

# Configure Terminal Experience
[private]
configure-terminal:
@/usr/libexec/configure-terminal.sh

# Enable Cockpit for web-based system management | https://cockpit-project.org/
cockpit:
Expand Down
35 changes: 17 additions & 18 deletions usr/libexec/bluefin-cli.sh → usr/libexec/configure-terminal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ source /usr/lib/ujust/ujust.sh
###
targets=(
"bluefin-cli"
"bluefin-dx-cli"
"fedora-toolbox"
"ubuntu-toolbox"
"wolfi-toolbox"
"wolfi-dx-toolbox"
)

###
Expand Down Expand Up @@ -39,6 +41,7 @@ function String_check(){
###
# Trap function
###
# shellcheck disable=SC2317
function ctrl_c(){
printf "\nSignal SIGINT caught\n"
Exiting
Expand Down Expand Up @@ -66,7 +69,6 @@ function Make_container(){
fi
if test "$MAKE_CONTAINER" -eq 1; then
printf "Not making a container. Existing containers will not be deleted...\n"
Make_bashrc_d_file "$TERMINAL_CHOICE" "$CONTAINER_CHOICE"
Good_Exit
fi
}
Expand Down Expand Up @@ -124,7 +126,6 @@ function Is_enabled_and_stop(){
unset "$Enabled"
done
if test "$MATCH" -eq 1; then
Make_bashrc_d_file "$TERMINAL_CHOICE" "$1"
Good_Exit
fi
unset $MATCH
Expand All @@ -148,7 +149,6 @@ function Already_exists_and_rm(){
podman rm --force "$1"
else
echo "Reuisng existing $1 container."
Make_bashrc_d_file "$TERMINAL_CHOICE" "$1"
Good_Exit
fi
unset "$Delete"
Expand Down Expand Up @@ -194,20 +194,20 @@ function Build_container(){
# If ~/.bashrc.d exists and Chose Container for terminal. Make a symlink from /usr/share/ublue-os for first time shell.
# If Host was chosen. Remove existing symlink.
###
function Make_bashrc_d_file(){
if test -d "${HOME}/.bashrc.d" && test "$1" = "Host"; then
echo "${red}Removing existing ~/.bashrc.d/00-container.sh if it exists${normal}."
test -f "${HOME}/.bashrc.d/00-container.sh" && rm "${HOME}/.bashrc.d/00-container.sh"
elif test -d "${HOME}/.bashrc.d"; then
echo "Setting first terminal be Container for bash using ~/.bashrc.d"
echo "Enter into container using prompt's menu after first entry"
echo "${blue}This requires your bash shell to source files in ~/.bashrc.d/${normal}"
test -e "${HOME}/.bashrc.d/00-container.sh" && rm "${HOME}/.bashrc.d/00-container.sh"
cp "/usr/share/ublue-os/bluefin-cli/${2}.sh" "${HOME}/.bashrc.d/00-container.sh"
else
echo "${red}Not implemented for non-Bash shells${normal} at this time..."
fi
}
# function Make_bashrc_d_file(){
# if test -d "${HOME}/.bashrc.d" && test "$1" = "Host"; then
# echo "${red}Removing existing ~/.bashrc.d/00-container.sh if it exists${normal}."
# test -f "${HOME}/.bashrc.d/00-container.sh" && rm "${HOME}/.bashrc.d/00-container.sh"
# elif test -d "${HOME}/.bashrc.d"; then
# echo "Setting first terminal be Container for bash using ~/.bashrc.d"
# echo "Enter into container using prompt's menu after first entry"
# echo "${blue}This requires your bash shell to source files in ~/.bashrc.d/${normal}"
# test -e "${HOME}/.bashrc.d/00-container.sh" && rm "${HOME}/.bashrc.d/00-container.sh"
# cp "/usr/share/ublue-os/bluefin-cli/${2}.sh" "${HOME}/.bashrc.d/00-container.sh"
# else
# echo "${red}Not implemented for non-Bash shells${normal} at this time..."
# fi
# }

function main(){
trap ctrl_c SIGINT
Expand All @@ -219,7 +219,6 @@ function main(){
Is_enabled_and_stop "$CONTAINER_CHOICE"
Already_exists_and_rm "$CONTAINER_CHOICE"
Build_container "$MAKE_CONTAINER" "$CONTAINER_MANAGER" "$CONTAINER_CHOICE"
Make_bashrc_d_file "$TERMINAL_CHOICE" "$CONTAINER_CHOICE"
Good_Exit
}

Expand Down
57 changes: 57 additions & 0 deletions usr/libexec/enable-bluefin-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/bash

# Choose to enable or disable bluefin-cli

# shellcheck disable=1091
# shellcheck disable=2206
# shellcheck disable=2154
source /usr/lib/ujust/ujust.sh

bluefin_cli=(${red}Disabled${n} ${red}Inactive${n})

function get_status(){
if systemctl --quiet --user is-enabled bluefin-cli.target; then
bluefin_cli[0]="${green}Enabled${n}"
else
bluefin_cli[0]="${red}Disabled${n}"
fi
if systemctl --quiet --user is-active bluefin-cli.service; then
bluefin_cli[1]="${green}Active${n}"
else
bluefin_cli[1]="${red}Inactive${n}"
fi
echo "Bluefin-cli is currently ${b}${bluefin_cli[0]}${n} and ${b}${bluefin_cli[1]}${n}"
}
function logic(){
if test "$toggle" = "Enable"; then
echo "${b}${green}Enabling${n} Bluefin-CLI"
systemctl --user enable --now bluefin-cli.target > /dev/null 2>&1
if ! systemctl --quiet --user is-active bluefin-cli.service; then
systemctl --user reset-failed bluefin-cli.service > /dev/null 2>&1 || true
echo "${b}${green}Starting${n} Bluefin-CLI"
systemctl --user start bluefin-cli.service
fi
elif test "$toggle" = "Disable"; then
echo "${b}${red}Disabling${n} Bluefin-CLI"
systemctl --user disable --now bluefin-cli.target > /dev/null 2>&1
if systemctl --quiet --user is-active bluefin-cli.service; then
echo "Do you want to ${b}${red}Stop${n} the Container?"
stop=$(Confirm)
if test "$stop" -eq 0; then
systemctl --user stop bluefin-cli.service > /dev/null 2>&1
systemctl --user reset-failed bluefin-cli.service > /dev/null 2>&1 || true
fi
fi
else
echo "Not Changing"
fi
}

function main(){
get_status
toggle=$(Choose Enable Disable Cancel)
logic
get_status
}

main
29 changes: 29 additions & 0 deletions usr/libexec/prompt-add-profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/bash

# Read the current value of the array
CURRENT_VALUE=$(dconf read /org/gnome/Prompt/profile-uuids)
guid=$1

# remove the leading and trailing brackets
CURRENT_VALUE=${CURRENT_VALUE:1:-1}

# remove any spaces
CURRENT_VALUE=${CURRENT_VALUE// /}

# split the string into an array
IFS=',' read -r -a array <<<"$CURRENT_VALUE"

# add the new value
array+=("'$guid'")

# join the array back into a string
UPDATED_VALUE=$(printf "%s," "${array[@]}")

# remove the trailing comma
UPDATED_VALUE=${UPDATED_VALUE%?}

# add the leading and trailing brackets
UPDATED_VALUE="[$UPDATED_VALUE]"

# Write the updated array back to dconf
dconf write /org/gnome/Prompt/profile-uuids "$UPDATED_VALUE"
42 changes: 42 additions & 0 deletions usr/libexec/prompt-create-profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/bash

# create a Prompt profile using dconf given the guid of the instance
# $1 = name

# dconf read /org/gnome/Prompt/Profiles/d092b3519698570a3252762c658f7629/
# /org/gnome/Prompt/Profiles/d092b3519698570a3252762c658f7629/custom-command
# 'blincus shell myubuntu'
# /org/gnome/Prompt/Profiles/d092b3519698570a3252762c658f7629/label
# 'myubuntu'
# /org/gnome/Prompt/Profiles/d092b3519698570a3252762c658f7629/login-shell
# true
# /org/gnome/Prompt/Profiles/d092b3519698570a3252762c658f7629/use-custom-command
# true

# if dconf doesn't exist, just return
if ! command -v dconf >/dev/null; then
return
fi

# shellcheck disable=SC2001
gen_uuid() {
uuid="$(cat /proc/sys/kernel/random/uuid)"
echo "$uuid" | sed 's/-//g'
}

guid=$(gen_uuid)
name="$1"
palette="$2"

profile="/org/gnome/Prompt/Profiles/${guid}/"

dconf write "${profile}custom-command" "'sh -c \"[ ! -e /run/.containerenv ] && [ ! -e /run/.dockerenv ] && distrobox enter ${name} || ${SHELL}\"'"
dconf write "${profile}label" "'${name}'"
dconf write "${profile}use-custom-command" "true"
if test -n "$palette"; then
dconf write "${profile}palette" "'${palette}'"
elif test "$name" = "bluefin-cli"; then
dconf write "${profile}palette" "'catppuccin-dynamic'"
fi

/usr/libexec/prompt-add-profile.sh "$guid"
52 changes: 52 additions & 0 deletions usr/libexec/prompt-remove-profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/bash
# ensure that the prompt profiles for deleted instances are removed

# if dconf doesn't exist, just return
if ! command -v dconf >/dev/null; then
return
fi

name="$1"

# Read the current value of the array
CURRENT_VALUE=$(dconf read /org/gnome/Prompt/profile-uuids)

# remove the leading and trailing brackets
CURRENT_VALUE=${CURRENT_VALUE:1:-1}

# remove any spaces
CURRENT_VALUE=${CURRENT_VALUE// /}

# split the string into an array
IFS=',' read -r -a array <<<"$CURRENT_VALUE"

# loop through the array and remove any that don't exist
for i in "${!array[@]}"; do
guid=${array[i]}

# remove single quotes from guid

guid=${guid//\'/}

#echo "Checking profile for $(red $guid)"
profile="/org/gnome/Prompt/Profiles/${guid}/"

custom_shell=$(dconf read "${profile}custom-command")

if [[ $custom_shell == *"[ ! -e /run/.containerenv ] && [ ! -e /run/.dockerenv ] && distrobox enter ${name}"* ]]; then
dconf reset -f "${profile}"
# remove the guid from the array
unset 'array[i]'
# join the array back into a string
UPDATED_VALUE=$(printf "%s," "${array[@]}")

# remove the trailing comma
UPDATED_VALUE=${UPDATED_VALUE%?}

# add the leading and trailing brackets
UPDATED_VALUE="[$UPDATED_VALUE]"

# Write the updated array back to dconf
dconf write /org/gnome/Prompt/profile-uuids "$UPDATED_VALUE"
fi
done
6 changes: 0 additions & 6 deletions usr/share/ublue-os/bluefin-cli/bluefin-cli.sh

This file was deleted.

6 changes: 6 additions & 0 deletions usr/share/ublue-os/bluefin-cli/prompt-integration
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


[Service]
ExecStartPre=-/usr/libexec/prompt-remove-profile.sh %N
ExecStartPost=-/usr/libexec/prompt-create-profile.sh %N
ExecStop=-/usr/libexec/prompt-remove-profile.sh %N

0 comments on commit 61ea499

Please sign in to comment.