From 93e87c6d288f1c67f63e40db5ee2cefabe2800fd Mon Sep 17 00:00:00 2001 From: Tearran Date: Fri, 12 Apr 2024 00:32:27 -0700 Subject: [PATCH] Renamed scripts configng and armbian-interface to confignng+ cli / tui --- bin/armbian-configng | 244 +++++------------------- bin/configng | 78 -------- bin/configng-cli | 222 +++++++++++++++++++++ bin/{armbian-interface => configng-tui} | 0 4 files changed, 272 insertions(+), 272 deletions(-) mode change 100755 => 100644 bin/armbian-configng delete mode 100644 bin/configng create mode 100755 bin/configng-cli rename bin/{armbian-interface => configng-tui} (100%) diff --git a/bin/armbian-configng b/bin/armbian-configng old mode 100755 new mode 100644 index 2a6c89774..1ae72391c --- a/bin/armbian-configng +++ b/bin/armbian-configng @@ -1,222 +1,78 @@ #!/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 +tput init # -# 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) - +# Language-based variable assignment for script directory path +# This serves as a Rosetta Stone for developers, +# allowing them to use the variable name they are most comfortable with. -# -# Consept Distribution Compatibility checks -check_distro() { +# allows CTRL c to exit +trap "exit" INT TERM - [[ -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%%-*}" +# Get the script directory +script_dir="$(dirname "$0")" -} - -[[ "$1" == "--dev" ]] && dev=1 && shift 1 +# Define the lib directory one level up from the script directory +lib_dir="$script_dir/../lib/armbian-configng" +doc_dir="$script_dir/../share/doc/armbian-configng" +# Check for the existence of the config.ng.jobs.json file in the lib directory +json_file="$lib_dir/config.ng.jobs.json" # -# Check if the script is dev version. -suffix="${file_name##*-}" +# Load The Bash procedure Objects +json_data=$(cat "$json_file") -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 +# +# We're checking if the 'whiptail' command is available on the system. +# 'whiptail' is a simple dialog box utility that works well with Bash. It doesn't have all the features of some other dialog box utilities, but it does everything we need for this script. +# If 'whiptail' is available, we assign its name to the variable DIALOG. This way, we can easily switch to another dialog box utility in the future if we need to, just by changing this one line of code. +[[ -x "$(command -v whiptail)" ]] && DIALOG="whiptail" -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 +# +# Prepare the module options array +declare -A module_options -fi # -# Check if the script is being run as root -# UX Development mode bypasses root check, many functions will not work as expected +# Load configng core functions and module options array -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 +source "$lib_dir/config.ng.functions.sh" +set_runtime_variables +echo "Loaded Runtime variables..." | show_infobox ; +set_newt_colors 2 +echo "Loaded Dialog..." | show_infobox ; +source "$lib_dir/config.ng.docs.sh" +echo "Loaded Docs..." | show_infobox ; +source "$lib_dir/config.ng.network.sh" +echo "Loaded Network helpers..." | show_infobox ; -# -# 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 +# Loads the varibles from beta armbian-config for runtime handeling +source "$lib_dir/config.ng.runtime.sh" ; +echo "Loaded Runtime conditions..." | show_infobox ; # -# 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 +# if not sudo +# Runtime "include this script" for USER and development setup condistion +if [[ $EUID != 0 ]]; then + source "$lib_dir/config.ng.runtime.dev.sh" ; + echo "Loaded Develoment +Runtime conditions..." | show_infobox ; + fi -} +tput clear # -# 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 +# Generate the top menu with the modified Object data +while generate_top_menu "$json_data"; do tput clear ; done -} - -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 \ No newline at end of file + +# +# Exit the script with a success status code +exit 0 diff --git a/bin/configng b/bin/configng deleted file mode 100644 index 1ae72391c..000000000 --- a/bin/configng +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/bash - - -tput init -# -# Language-based variable assignment for script directory path -# This serves as a Rosetta Stone for developers, -# allowing them to use the variable name they are most comfortable with. - -# allows CTRL c to exit -trap "exit" INT TERM - -# Get the script directory -script_dir="$(dirname "$0")" - -# Define the lib directory one level up from the script directory -lib_dir="$script_dir/../lib/armbian-configng" -doc_dir="$script_dir/../share/doc/armbian-configng" -# Check for the existence of the config.ng.jobs.json file in the lib directory -json_file="$lib_dir/config.ng.jobs.json" - -# -# Load The Bash procedure Objects -json_data=$(cat "$json_file") - - -# -# We're checking if the 'whiptail' command is available on the system. -# 'whiptail' is a simple dialog box utility that works well with Bash. It doesn't have all the features of some other dialog box utilities, but it does everything we need for this script. -# If 'whiptail' is available, we assign its name to the variable DIALOG. This way, we can easily switch to another dialog box utility in the future if we need to, just by changing this one line of code. -[[ -x "$(command -v whiptail)" ]] && DIALOG="whiptail" - - -# -# Prepare the module options array -declare -A module_options - - -# -# Load configng core functions and module options array - - -source "$lib_dir/config.ng.functions.sh" -set_runtime_variables -echo "Loaded Runtime variables..." | show_infobox ; -set_newt_colors 2 -echo "Loaded Dialog..." | show_infobox ; -source "$lib_dir/config.ng.docs.sh" -echo "Loaded Docs..." | show_infobox ; -source "$lib_dir/config.ng.network.sh" -echo "Loaded Network helpers..." | show_infobox ; - - -# -# Loads the varibles from beta armbian-config for runtime handeling - -source "$lib_dir/config.ng.runtime.sh" ; -echo "Loaded Runtime conditions..." | show_infobox ; - -# -# if not sudo -# Runtime "include this script" for USER and development setup condistion -if [[ $EUID != 0 ]]; then - source "$lib_dir/config.ng.runtime.dev.sh" ; - echo "Loaded Develoment -Runtime conditions..." | show_infobox ; - -fi - -tput clear -# -# Generate the top menu with the modified Object data -while generate_top_menu "$json_data"; do tput clear ; done - - -# -# Exit the script with a success status code -exit 0 diff --git a/bin/configng-cli b/bin/configng-cli new file mode 100755 index 000000000..2a6c89774 --- /dev/null +++ b/bin/configng-cli @@ -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 \ No newline at end of file diff --git a/bin/armbian-interface b/bin/configng-tui similarity index 100% rename from bin/armbian-interface rename to bin/configng-tui