DistroHopper/quickfzf

94 lines
2.7 KiB
Text
Raw Normal View History

2023-09-14 13:25:34 +00:00
#!/usr/bin/bash
# Author: zenobit
# Description: Uses fzf to provide a simple TUI for quickemu and quickget
# License MIT
# Define variables
progname="${progname:="${0##*/}"}"
version="0.23"
#EDITOR="nano"
2023-09-14 13:25:34 +00:00
vms=(*.conf)
# Set traps to catch the signals and exit gracefully
trap "exit" INT
trap "exit" EXIT
# Dependency check: check if fzf is installed and can be executed
if ! command -v fzf >/dev/null 2>&1; then
echo "You are missing fzf..." && exit 255
2023-09-14 13:25:34 +00:00
fi
if ! command -v quickemu >/dev/null 2>&1; then
echo "You are missing quickemu..." && exit 255
2023-09-14 13:25:34 +00:00
fi
QUICKGET=$(command -v quickget) || exit 255
# Display header
printf '%s: v.%s\nquickemu: v.%s\n' "$progname" "$version" "$(quickemu --version)"
if [ -z "$EDITOR" ]
then
echo "editor: Not set! edit configs will not work!"
else
echo "editor: $EDITOR"
fi
printf '\n Workdir: %s\n\n Prepared VMs:\n-------------\n' "$(pwd)"
2023-09-14 13:25:34 +00:00
# Check if there are any VMs
if [ ${#vms[@]} -eq 0 ]; then
echo "No VMs found."
exit 1
fi
# Print the names of the available VMs
printf "%s\n" "${vms[@]%.*}"
echo "-------------"
printf 'Press CTRL+c anytime to kill %s\n\n' "$progname"
2023-09-14 13:25:34 +00:00
# Action prompt
printf " Do you want to create a new VM? (c)
edit VM's config file (e)
2023-09-14 13:25:34 +00:00
or run an existing one? (press anything)\n"
read -rn 1 -s start
case $start in
c ) todo="create";;
e ) todo="edit";;
2023-09-14 13:25:34 +00:00
esac
# If the user chose to create a new VM
if [ "$todo" = "create" ]; then
os=$(quickget | sed 1d | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose OS to download')
fi
# Get the release and edition to download, if necessary
choices=$(quickget "$os" | sed 1d)
if [ "$todo" = "edit" ]; then
editconfig=$(ls | grep '.conf' | fzf --cycle --header='Choose config to edit')
"$EDITOR" "$editconfig"
elif [ "$(echo "$choices" | wc -l)" = 1 ]; then
# get release
release=$(echo "$choices" | grep 'Releases' | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose Release')
# downloading
printf '\n Trying to download %s %s...\n\n' "$os" "$release"
quickget "$os" "$release"
else
# get release
release=$(echo "$choices" | grep 'Releases' | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose Release')
# get edition
edition=$(echo "$choices" | grep 'Editions' | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose Edition')
# downloading
printf '\n Trying to download %s %s %s...\n\n' "$os" "$release" "$edition"
quickget "$os" "$release" "$edition"
2023-09-14 13:25:34 +00:00
fi
2023-09-14 13:25:34 +00:00
# choose VM to run
chosen=$(echo "$(ls *.conf 2>/dev/null | sed 's/\.conf$//')" | fzf --cycle --header='Choose VM to run')
2023-09-14 13:25:34 +00:00
# Run chosen VM
printf '\n Starting %s...\n\n' "$chosen"
quickemu -vm "$chosen.conf"
exit 0