mirror of
https://github.com/optim-enterprises-bv/secureblue.git
synced 2025-11-06 05:17:45 +00:00
204 lines
5.6 KiB
Bash
Executable File
204 lines
5.6 KiB
Bash
Executable File
#!/usr/bin/bash
|
|
|
|
# Copyright 2024 Universal Blue
|
|
#
|
|
# This file includes code that is licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software distributed under the License is
|
|
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
|
|
##################################################################
|
|
# This is a helper script to provide a basic fallback replacement
|
|
# for just commands and bash scripts that want to use gum in uBlue
|
|
##################################################################
|
|
|
|
# Supported menu handlers
|
|
SUPPORTED_HANDLERS=(
|
|
"fzf"
|
|
)
|
|
|
|
# Check if gum is present
|
|
GUM=$(which gum 2>/dev/null)
|
|
|
|
# Check if fzf is installed and set it as the handler
|
|
FALLBACK_HANDLER=$(which fzf 2>/dev/null)
|
|
HANDLER=""
|
|
if [[ -n $FALLBACK_HANDLER ]]; then
|
|
HANDLER="fzf"
|
|
fi
|
|
|
|
# If $MENU is set
|
|
if [[ -n $MENU ]]; then
|
|
for BIN in "${SUPPORTED_HANDLERS[@]}"
|
|
do
|
|
if [[ "$BIN" == "$MENU" ]]; then
|
|
HANDLER=$BIN
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# Logic for what to do if gum is not installed
|
|
function noGum () {
|
|
if [[ -z "$1" ]]; then
|
|
# If no arguments are provided then error with
|
|
echo "ugum supports only choose or confirm as the first argument!"
|
|
echo "Usage:"
|
|
echo ' ugum choose option1 option2 \"option 3\"'
|
|
echo ' Returns: selected string'
|
|
echo ' ugum confirm "this is an optional question'
|
|
echo ' Returns: exit code in $? will be 0 for YES and 1 for NO'
|
|
exit 5
|
|
elif [[ "$1" == "choose" ]]; then
|
|
# If choose is the verb then run the choose function and pass all remaining args to an appropriate handler
|
|
if [[ "$HANDLER" == "fzf" ]]; then
|
|
# Use fzf for choice selector
|
|
choose_Fzf "${@:2}"
|
|
else
|
|
# Use generic bash selector
|
|
choose_Generic "${@:2}"
|
|
fi
|
|
elif [[ "$1" == "confirm" ]]; then
|
|
# If confirm is the verb then run the confirm function and pass all remaining args to an appropriate handler
|
|
if [[ "$HANDLER" == "fzf" ]]; then
|
|
# Use fzf as a confirm dialog
|
|
confirm_Fzf "${@:2}"
|
|
else
|
|
# Use a generic bash dialog
|
|
confirm_Generic "${@:2}"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Implements a generic bash choice dialog
|
|
function choose_Generic () {
|
|
# Change PS3 to our select prompt
|
|
PS3='Please enter your choice: '
|
|
|
|
# Make an array to contain all options in
|
|
OPTIONS=()
|
|
|
|
# Parse the arguments for the ones we support and care about
|
|
for arg in "$@"
|
|
do
|
|
# If the argument does not start with -
|
|
if [[ ! $arg =~ ^- ]]; then
|
|
OPTIONS+=("$arg")
|
|
fi
|
|
done
|
|
|
|
# Make a select prompt in bash
|
|
select opt in "${OPTIONS[@]}"
|
|
do
|
|
case $opt in
|
|
"")
|
|
# Invalid options print to STDERR and then loops back for the user to select again
|
|
echo "Invalid option $REPLY" >&2
|
|
;;
|
|
"$opt")
|
|
echo "$opt"
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
# Implements a choice dialog using fzf
|
|
function choose_Fzf () {
|
|
# Change our select prompt
|
|
PROMPT='Please select your choice: '
|
|
|
|
# Make an array to contain all options in
|
|
local OPTIONS
|
|
|
|
# Parse the arguments for the ones we support and care about
|
|
for arg in "$@"
|
|
do
|
|
# If the argument does not start with -
|
|
if [[ ! $arg =~ ^- ]]; then
|
|
if [[ "$OPTIONS" == "" ]]; then
|
|
OPTIONS="${arg}"
|
|
continue
|
|
fi
|
|
OPTIONS="${OPTIONS}\n${arg}"
|
|
fi
|
|
done
|
|
|
|
# Make a select prompt using fzf
|
|
echo -e "$OPTIONS" | fzf --layout=reverse --height="~20%" --prompt="$PROMPT"
|
|
}
|
|
|
|
# Implements a generic bash confirm dialog
|
|
function confirm_Generic () {
|
|
# Set default prompt
|
|
PROMPT="Are you sure?"
|
|
|
|
# Parse the arguments for the ones we support and care about
|
|
for arg in "$@"
|
|
do
|
|
if [[ ! $arg =~ ^- ]]; then
|
|
PROMPT="$arg"
|
|
fi
|
|
done
|
|
|
|
# Print the prompt and read input
|
|
read -r -p "$PROMPT [Y/n]: " YESNO
|
|
confirm_Parse "$YESNO"
|
|
}
|
|
|
|
# Implements a confirm dialog in fzf
|
|
function confirm_Fzf () {
|
|
PROMPT=$(confirm_getPrompt "$@")
|
|
|
|
# Make the confirm prompt using fzf and read response
|
|
YESNO=$(echo -e "Yes\nNo" | fzf --layout=reverse --height="~20%" --prompt="$PROMPT ")
|
|
confirm_Parse "$YESNO"
|
|
}
|
|
|
|
# Gets the prompt for the confirm dialog, with a fallback to "Are you sure?"
|
|
function confirm_getPrompt () {
|
|
# Set default prompt
|
|
PROMPT="Are you sure?"
|
|
|
|
# Parse the arguments for the ones we support and care about
|
|
for arg in "$@"
|
|
do
|
|
if [[ ! $arg =~ ^- ]]; then
|
|
PROMPT="$arg"
|
|
fi
|
|
done
|
|
|
|
# Return the prompt
|
|
echo "$PROMPT"
|
|
}
|
|
|
|
# Parse the confirm response and translate it the same exit codes gum uses
|
|
function confirm_Parse () {
|
|
case "$@" in
|
|
[Yy]*)
|
|
# Use exit code 0 for yes, just like gum
|
|
exit 0
|
|
;;
|
|
[Nn]*)
|
|
# Use exit code 1 for no, just like gum
|
|
exit 1
|
|
;;
|
|
*)
|
|
# Default exit code is 0
|
|
exit 0
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# If gum is not present
|
|
if [[ -z "$GUM" ]]; then
|
|
noGum "$@"
|
|
else
|
|
# If gum is present just pass args to gum
|
|
$GUM "$@"
|
|
fi |