-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tearran
committed
Apr 4, 2024
1 parent
5e2b91e
commit 5e8327b
Showing
43 changed files
with
8,003 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
#!/bin/bash | ||
|
||
# This script provides a command-line interface for managing Armbian configuration. | ||
# It loads libraries of functions from the lib directory and displays them in a menu. | ||
# The user can select a function to run, or use a text-based user interface (TUI) to navigate the menus. | ||
# The script also provides a help option and a debug option for developers. | ||
# The script requires sudo privileges to run some functions. | ||
# The script uses whiptail or dialog for the TUI, depending on which is available. | ||
|
||
#set -x | ||
#set -e | ||
|
||
# | ||
# Enable Dynamic directory root use home ~/ , /bin , /usr/sbin etc.. | ||
bin="$(dirname "${BASH_SOURCE[0]}")" | ||
directory="$(cd "$bin/../" && pwd )" | ||
file_name="$(basename "${BASH_SOURCE[0]}")" | ||
filename="${file_name%.*}" | ||
libpath=$(cd "$directory/lib/$filename/" && pwd) | ||
#sharepath=$(cd "$directory/share/${filename%-dev}/" && pwd) | ||
|
||
|
||
# | ||
# Consept Distribution Compatibility checks | ||
check_distro() { | ||
|
||
[[ -f "/usr/bin/${filename%%-*}-config" ]] && distro_config="${filename%%-*}" | ||
[[ -f "/etc/${filename%%-*}-release" ]] && distro_release="${filename%%-*}" | ||
# if both true then we are good to go | ||
[[ -z "$distro_config" ]] || [[ -z "$distro_release" ]] && echo "W: Costum build, Tech support links are missing." | ||
[[ -n "$distro_config" ]] && [[ -n "$distro_release" ]] && echo "I: This build seems to be community supported" | ./armbian-interface -o | ||
[[ -f "/usr/sbin/${filename%%-*}-config" ]] && distro_config="${filename%%-*}" | ||
[[ -f "/etc/${filename%%-*}-release" ]] && distro_release="${filename%%-*}" | ||
|
||
} | ||
|
||
[[ "$1" == "--dev" ]] && dev=1 && shift 1 | ||
|
||
# | ||
# Check if the script is dev version. | ||
suffix="${file_name##*-}" | ||
|
||
if [[ "$suffix" == dev ]]; then | ||
dev=1 | ||
check_distro #| armbian-interface -o | ||
fi | ||
|
||
if [[ "$(id -u)" != "0" ]] && [[ "$dev" == "1" ]] ; then | ||
|
||
cat << EOF #| ./armbian-interface -o | ||
I: Running in UX development mode | ||
W: Admin functions will not work as expected | ||
EOF | ||
elif [[ "$(id -u)" == "0" ]] && [[ "$dev" == "1" ]] ; then | ||
cat << EOF | ./armbian-interface -o | ||
I: Running in UX development mode | ||
W: Document files may need Admin privleges to edit/remove | ||
EOF | ||
|
||
fi | ||
|
||
# | ||
# Check if the script is being run as root | ||
# UX Development mode bypasses root check, many functions will not work as expected | ||
|
||
if [[ "$(id -u)" != "0" ]] && [[ "$dev" != "1" ]]; then | ||
echo -e "E: This tool requires root privileges. Try: \"sudo $filename\"" >&2 | ||
exit 1 | ||
fi | ||
|
||
declare -A dialogue | ||
|
||
# | ||
# Check if whiptail or dialog is installed and set the variable 'dialogue' accordingly. | ||
# todo add a fallback TUI and GUI | ||
if command -v whiptail &> /dev/null; then | ||
dialogue="whiptail" | ||
elif command -v dialog &> /dev/null; then | ||
dialogue="dialog" | ||
else | ||
echo "TUI not found" | ||
echo "Warning: Using fallback TUI" | ||
sleep 1 | ||
clear && generate_read | ||
fi | ||
|
||
source "$libpath/functions.sh" | ||
source "$libpath/documents.sh" | ||
for file in "$libpath"/*/*.sh; do | ||
source "$file" | ||
done | ||
|
||
# | ||
# mapfile -t categories < <(ls -d "$libpath"/* ) | ||
mapfile -t categories < <(find "$libpath"/* -type d) | ||
declare -A functions | ||
|
||
for category in "${categories[@]}"; do | ||
category_name="${category##*/}" | ||
|
||
category_file="$category/readme.md" | ||
if [[ -f "$category_file" ]]; then | ||
category_description=$(grep -oP "(?<=# @description ).*" "$category_file") | ||
fi | ||
|
||
for file in "$category"/*.sh; do | ||
description="" | ||
while IFS= read -r line; do | ||
if [[ $line =~ ^#\ @description\ (.*)$ ]]; then | ||
description="${BASH_REMATCH[1]}" | ||
elif [[ $line =~ ^function\ (.*::.*)\(\)\{$ ]]; then | ||
# END: be15d9bcejpp | ||
function_name="${BASH_REMATCH[1]}" | ||
key="$category_name:${file##*/}:${function_name}" | ||
functions["$key,function_name"]=$(echo "$function_name" | sed 's/.*:://') | ||
functions["$key,group_name"]=$(echo "$function_name" | sed 's/::.*//') | ||
functions["$key,description"]=$description | ||
elif [[ $line =~ ^#\ @options\ (.*)$ ]]; then | ||
functions["$key,options"]="${BASH_REMATCH[1]}" | ||
fi | ||
done < "$file" | ||
functions["$key,category"]=$category_name | ||
functions["$key,category_description"]=$category_description | ||
done | ||
done | ||
|
||
|
||
# | ||
# WIP: Check arguments for no flag options | ||
# armbian-config --help | ||
# Change to BASH: /usr/sbin/armbian-config main=System selection=BASH | ||
handle_no_flag(){ | ||
if [[ "$1" == *"="* ]]; then | ||
IFS='=' read -r key value <<< "$1" | ||
function_name=$(parse_action "$key" "$value") | ||
# Call the function using variable indirection | ||
${function_name} | ||
elif [[ "$1" == "help"* ]]; then | ||
generate_list_cli | ||
fi | ||
} | ||
|
||
# | ||
# Check arguments for long flag options | ||
# Help message related to the functions the back end | ||
handle_long_flag(){ | ||
if [[ "$1" == "--help" ]]; then | ||
generate_list_run | ||
exit 0 ; | ||
elif [[ "$1" == "--doc" ]]; then | ||
generate_doc | ||
exit 0 ; | ||
fi | ||
# WIP: | ||
if [ "$1" == "--run" ]; then | ||
shift # Shifts the arguments to the left, excluding the first argument ("-r") | ||
group_name="$1" # Takes the first argument as the group name | ||
shift 1 # Shifts the arguments again to exclude the group name | ||
|
||
function_name=$(parse_action "$group_name" "$1") | ||
if [ $? -eq 0 ]; then | ||
# Call the function using variable indirection | ||
${function_name} | ||
fi | ||
elif [ "$1" == "--help" ]; then | ||
generate_list_run | ||
exit | ||
elif [ "$1" == "--test" ]; then | ||
check_distro | armbian-interface -o && $1 > /dev/null | ||
fi | ||
|
||
} | ||
# | ||
# Check arguments for short flag options | ||
# THe interface help message | ||
handle_short_flag(){ | ||
if [ "$1" == "-h" ]; then | ||
generate_help | ||
exit 0 ; | ||
# Generate a text-based user interface | ||
elif [ "$1" == "-t" ] ; then | ||
generate_read ; exit 0 ; | ||
# Generate all doc files | ||
elif [ "$1" == "-d" ] ; then | ||
generate_doc ; exit 0 ; | ||
elif [ "$1" == "-j" ] ; then | ||
generate_json ; exit 0 ; | ||
fi | ||
|
||
} | ||
|
||
case "$1" in | ||
*"="*) | ||
# Handle the case where $1 contains "=" | ||
handle_no_flag "$@" | ||
;; | ||
*"--"*) | ||
# Handle the case where $1 starts with "--" | ||
handle_long_flag "$@" | ||
;; | ||
*"-"*) | ||
# Handle the case where $1 starts with "-" | ||
handle_short_flag "$1" | ||
;; | ||
*) | ||
handle_no_flag "$@" | ||
# Handle the case where $1 does not match any of the above patterns | ||
# You can add your code here | ||
;; | ||
esac | ||
|
||
if [[ -z "$1" ]] ; then | ||
while true; do | ||
generate_tui ; | ||
if [[ "$?" == "0" ]]; then | ||
exit 0 | ||
fi | ||
done | ||
|
||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#!/bin/bash | ||
|
||
# | ||
# Copyright (c) 2023 Joseph C Turner | ||
# All rights reserved. | ||
# | ||
# This script. | ||
# demonstrates the compatibility of multiple interfaces for displaying menus or messages. | ||
# It uses an array to set the options for all three menus (bash, whiptail, and dialog). | ||
# The script checks if whiptail or dialog are available on the system and uses them to display the menu in a more user-friendly way. | ||
# If neither of these programs is available, it falls back to using bash. | ||
# while both are installed falls back to whiptail to display the menu. | ||
# The user can override the default program by passing an argument when running the script: | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# 1. Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# | ||
# 2. Redistributions in binary form must reproduce the above copyright | ||
# notice, this list of conditions and the following disclaimer in the | ||
# documentation and/or other materials provided with the distribution. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
# | ||
|
||
## DIRECTORY variable to the absolute path of the script's directory | ||
# directory="$(dirname "$(readlink -f "$0")")" | ||
filename=$(basename "${BASH_SOURCE[0]}") | ||
|
||
## DIALOG variable to the absolute path of the script's directory | ||
DIALOG="bash" | ||
[[ -x "$(command -v dialog)" ]] && DIALOG="dialog" | ||
[[ -x "$(command -v whiptail)" ]] && DIALOG="whiptail" | ||
|
||
show_help(){ | ||
|
||
echo -e "\nUsage: [command] | ${filename%.*} [ -h | -m | -o ]" | ||
echo "Options:" | ||
echo " -h, Print this help." | ||
echo "" | ||
echo " -o, Opens an OK message Box" | ||
echo "" | ||
echo " -m, Opens an Menu select Box." | ||
echo "" | ||
echo " -p, Opens Popup message box. " | ||
echo "" | ||
exit 1; | ||
} | ||
|
||
show_message(){ | ||
|
||
# Read the input from the pipe continuously until there is no more input | ||
input="" | ||
while read -r line; do | ||
input+="$line\n" | ||
done | ||
|
||
# Display the "OK" message box with the input data | ||
[[ $DIALOG != "bash" ]] && $DIALOG --title "Message Box" --msgbox "$input" 0 0 | ||
[[ $DIALOG == "bash" ]] && echo -e "$input" | ||
[[ $DIALOG == "bash" ]] && read -p -r "Press [Enter] to continue..." ; echo "" ; exit 1 | ||
|
||
} | ||
|
||
show_popup(){ | ||
|
||
|
||
input="" | ||
while read -r line; do | ||
input+="$line\n" | ||
done | ||
|
||
[[ $DIALOG != "bash" ]] && $DIALOG --title "Popup Box" --infobox "$input" 0 0 | ||
[[ $DIALOG == "bash" ]] && echo -e "$input" | ||
|
||
} | ||
|
||
show_menu(){ | ||
|
||
|
||
# Get the input and convert it into an array of options | ||
inpu_raw=$(cat) | ||
# Remove the lines befor -h | ||
input=$(echo "$inpu_raw" | sed 's/-\([a-zA-Z]\)/\1/' | grep '^ [a-zA-Z] ' | grep -v '\[') | ||
options=() | ||
while read -r line; do | ||
package=$(echo "$line" | awk '{print $1}') | ||
description=$(echo "$line" | awk '{$1=""; print $0}' | sed 's/^ *//') | ||
options+=("$package" "$description") | ||
done <<< "$input" | ||
|
||
# Display the menu and get the user's choice | ||
[[ $DIALOG != "bash" ]] && choice=$($DIALOG --title "Menu" --menu "Choose an option:" 0 0 9 "${options[@]}" 3>&1 1>&2 2>&3) | ||
|
||
# Check if the user made a choice | ||
if [ $? -eq 0 ]; then | ||
echo "$choice" | ||
else | ||
echo "You cancelled." | ||
fi | ||
} | ||
|
||
[[ $1 == "-m" ]] && show_menu ; | ||
[[ $1 == "-o" ]] && show_message ; | ||
[[ $1 == "-h" ]] && show_help ; | ||
[[ $1 == "-p" ]] && show_popup ; | ||
[[ -z "$*" ]] && show_help ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.